2016-02-13 33 views
0

假設我有以下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)) 

如果我想提取以前trialcolor,我會怎麼做呢?最終的目標是與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 
+1

'內(DF,prevcolor < - 顏色[匹配(試用 - 1 ,trial)])'適用於你的例子,不確定普遍性 – rawr

回答

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 
+0

令人遺憾的是,循環在R中很慢。 – Nick

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,我們可以用​​拿到小組

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 
0

下面是一個使用簡單merge功能R. 你的數據框另一種解決方案:

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 
>