2

In my dataframe there is a column with "Sound" and "Response" as values. Ideally, the pattern is two Sounds followed by one Response. But, it can happen that there are three Sounds followed by a Response.

How can I tell R to raise a flag whenever it finds this pattern in my data? I need to look at each case individually before I can delete the third Sound-row.

>df <- data.frame(V1=rep("SN", 7),  
             V3=c("Sound", "Sound", "Response", "Sound", "Sound", "Sound", "Response"), 
             V4=c("XYZc02i03", "XYZq02i03", 200, "ZYXc01i30", "ZYXq01i30", "ZYXc01i35", 100), 
             stringsAsFactors=FALSE) 

V1       V3        V4
SN    Sound XYZc02i03
SN    Sound XYZq02i03
SN Response       200
SN    Sound ZYXc01i30
SN    Sound ZYXq01i30
SN    Sound ZYXc01i35
SN Response       100     

So, after finding three consecutive Sounds and deleting the last one of them (i. e. the one just before the folowing Response), I should have the desired pattern like this:

V1       V3        V4
SN    Sound XYZc02i03
SN    Sound XYZq02i03
SN Response       200
SN    Sound ZYXc01i30
SN    Sound ZYXq01i30
SN Response       100  

I'm sorry that I keep posting these basic questions. Any ideas are, as always, greatly appreciated!

4

2 に答える 2

4
cumsum(rle(df$V3)$lengths)[rle(df$V3)$lengths == 3]
[1] 6

これは、「Sound」が 3 番目の行にある位置のベクトルを返します。これで、それらを簡単に削除したり、これらの位置をマークする列を作成したりできます。

于 2012-06-14T17:53:27.263 に答える
2

おそらくもっと簡単な解決策がありますが、これでうまくいくと思います。

df <- data.frame(V1=rep("SN", 7),  
             V3=c("Sound", "Sound", "Response", "Sound", "Sound", "Sound", "Response"), 
             V4=c("XYZc02i03", "XYZq02i03", 200, "ZYXc01i30", "ZYXq01i30", "ZYXc01i35", 100), 
             stringsAsFactors=FALSE)

df

my.run <- rep(0,dim(df)[1])

if(df$V3[1]=='Sound') (my.run[1] = 1) else my.run[1] = 0

for (i in 2:dim(df)[1]) {

     if(df$V3[i]=='Sound') (my.run[i] = my.run[i-1] + 1) else my.run[i] = 0

}

df2 <- df[my.run < 3,]
df2
于 2012-06-14T17:54:22.793 に答える