选择特定值并在R

时间:2019-07-07 12:26:55

标签: r dataframe

我必须对我的R知识进行复杂的操作。我有这个data数据框:

trial <- c(1,2,3,4,5,6)
b_value = c("x","x","x","y","y","y")

key1 <- c("b","b","b","b","b","m")
key2 <- c("n","m","m","n",NA,"b")
key3 <- c("b",NA,"n",NA,NA,NA)
key4 <- c("b",NA,NA,NA,NA,NA)

dur1 <- c(2,3,5,6,0,0)
dur2 <- c(1,6,3,2,0,0)
dur3 <- c(2,3,5,0,0,0)
dur4 <- c(2,3,0,0,0,0)

data <- data.frame(trial,b_value, key1, key2, key3, key4, dur1, dur2, dur3, dur4)

这些数据指的是按键操作,例如,每次试验中key1(b,n或m)的持续时间为dur1。 我必须汇总每一行的每个键持续时间(例如 b ),并创建一个新的列,其总时间为 b 。对于每一行中的每个键。 我该怎么做?

2 个答案:

答案 0 :(得分:0)

这是一个涉及grouped = df4.groupby('T') for name, group in grouped: df12=df4.C[df4.diffpos== 1.0] fig=plt.figure() ax1=fig.add_subplot(111, label=name) group.plot(y=['C'],ax=ax1, color='r', lw=2.,figsize=(10,10)) ax1.plot(df12,'^', markersize=10, color='m',label='Buy') ax.plot(df4.C[df4.diffpos== -1.0], 'v', markersize=10, color='k',label='Sell') plt.legend(loc = 'best') plt.title(name) plt.show()

的简单解决方案
TimeInterval()

这将产生以下结果:

DateFormatter()

请注意两个tidyverse选项和library(tidyverse) data %>% gather(key="key_no", value="key", starts_with("key")) %>% gather(key="dur_no", value="duration", starts_with("dur")) %>% mutate(key_no = str_replace(key_no, "key", ""), dur_no = str_replace(dur_no, "dur", "")) %>% filter(dur_no == key_no) %>% # Assumes that the duration and the key have linked numbers arrange(trial, key) %>% group_by(trial, key) %>% summarise(total_duration = sum(duration)) 这两个选项,以确保您没有太多的行。

答案 1 :(得分:0)

使用基数R:

document.getElementById("submit").addEventListener("click", function(){
 console.log(document.getElementById("number").value)
});