如何遍历列表/数据框列表并在R中对其执行操作?

时间:2019-12-05 17:29:46

标签: r list loops

我有一个包含多个列表的大列表,并且想对每个列表执行一个操作。到目前为止,我已经尝试过:

groups <- split(dataframe, list(d$Subject,d$Group)) #here, I split the original dataframe into groups based on subject and group

for (i in groups[[]]){


w <- which(i$Discrimination == 1) #which values in the column are equal to 1

i$Discrimination[w[1]:nrow(i)]<-1 #set all values after the first '1' in the column equal to one

}

但是到目前为止,我无法使它正常工作。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

for (x in list) {
     for (i in x) {
         i$Discrimination <-  i$Reversals
         w <- which(dfs[[i]]$Discrimination == 1)
         i$Discrimination[w[1]:nrow(i)]<-1
     }
}

不确定要对列表中的每个项目做什么,但是第一个for循环遍历列表,第二个遍历列表中的每个项目