假设我有以下data.frame:
df <- data.frame(color = c("G","G","G","R","R","R","R","R","R","R","G","G"),
trial = c(1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4))
如果我想从之前的color
中提取trial
,我该怎么做?最终目标是最终得到一个data.frame:
color trial prevcolor
1 G 1 <NA>
2 G 1 <NA>
3 G 1 <NA>
4 R 2 G
5 R 2 G
6 R 2 G
7 R 3 R
8 R 3 R
9 R 3 R
10 R 3 R
11 G 4 R
12 G 4 R
答案 0 :(得分:1)
我们可以使用lag
(假设'试用'是有序的)
df$prevcolor <- with(df, lag(color, n=sum(trial==trial[1L])))
df
# color trial prevcolor
#1 G 1 <NA>
#2 G 1 <NA>
#3 G 1 <NA>
#4 R 2 G
#5 R 2 G
#6 R 2 G
#7 R 3 R
#8 R 3 R
#9 R 3 R
#10 R 3 R
#11 G 4 R
#12 G 4 R
评论中@ rawr解决方案的变体(如果'试用'不是数字列)
Un1 <- unique(df$trial)
with(df, color[match(factor(trial, levels= Un1, labels = c(NA, head(Un1,-1))), trial)])
使用dplyr
,我们可以使用group_indices
来获取群组的索引
library(dplyr)
df %>%
mutate(prev_color = color[match(group_indices_(.,.dots = 'trial')-1, trial)])
# color trial prev_color
#1 G 1 <NA>
#2 G 1 <NA>
#3 G 1 <NA>
#4 R 2 G
#5 R 2 G
#6 R 2 G
#7 R 3 R
#8 R 3 R
#9 R 3 R
#10 R 3 R
#11 G 4 R
#12 G 4 R
答案 1 :(得分:1)
以下是使用for
循环的解决方案:
df <- data.frame(color = c("G","G","G","R","R","R","R","R","R","R","G","G"),
trial = c(1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4))
# iterate through trial numbers
for (trial in unique(df$trial)) {
# select color of previous trial number
prev_color <- as.character(df$color[df$trial == trial - 1])[1]
# assign previous color to current trial number
df$prevcolor[df$trial == trial] <- prev_color
}
df
## color trial prevcolor
##1 G 1 <NA>
##2 G 1 <NA>
##3 G 1 <NA>
##4 R 2 G
##5 R 2 G
##6 R 2 G
##7 R 3 R
##8 R 3 R
##9 R 3 R
##10 R 3 R
##11 G 4 R
##12 G 4 R
答案 2 :(得分:0)
这是使用R中的简单merge
函数的另一种解决方案。
您的数据框:
df <- data.frame(color = c("G","G","G","R","R","R","R","R","R","R","G","G"),
trial = c(1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4))
现在使用merge
功能。它仅用于合并数据帧。因此:
df2<-merge(data.frame(prevtrial=c(df$trial-1)),unique(df), by.x="prevtrial",by.y="trial",all.x=T)
现在为您的输出创建一个新的数据框:
newdf<-data.frame(color=df$color,trial=df$trial,prevtrial=df2$prevtrial,prevcolor=df2$color)
将给出:
> newdf
color trial prevtrial prevcolor
1 G 1 0 <NA>
2 G 1 0 <NA>
3 G 1 0 <NA>
4 R 2 1 G
5 R 2 1 G
6 R 2 1 G
7 R 3 2 R
8 R 3 2 R
9 R 3 2 R
10 R 3 2 R
11 G 4 3 R
12 G 4 3 R
>