通过在R中分配新数据帧来循环数据帧

时间:2018-01-10 14:52:39

标签: r loops for-loop

我试图循环R:

中的以下数据帧列表
ll <- list(week_32, week_33, week_34, week_35, week_36, week_37, week_38, week_39, week_40, week_41, week_42, week_43, week_44, week_45, week_46, week_47, week_48, week_49)

首先,我想删除两列中的重复项:

lapply( ll, function(x){x= x[!duplicated(x[,c("PZS_Id", "VL_Id")]),]})

这一切都很好。在随后的步骤中,我想dcast列表中的每个元素并将结果保存在新文件中。我的试用版如下,但我收到以下错误:错误:输入中找不到value.var(VL_Status)

`for (x in 32:max(data$week)){nam <- paste("week", "cast", x, sep="_")assign(nam, dcast(noquote(paste("week",x,sep="_")),PZS_Id + ï..Datum + Umfang.Id + PZS_Status + Date + week ~ VL_Id, value.var= 'VL_Status' ))}`

我认为这是因为noquote(paste("week",x,sep="_"))无法被识别为数据帧。有没有人知道如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

尝试eval(parse(text =nam))

最小例子:

library(reshape2)
week_32 <- data.frame(id = 1, column1 = sample(LETTERS,10), column2 = runif(10))
x<-32
nam <-noquote(paste("week",x,sep="_"))
new <- dcast(eval(parse(text =nam)),  id  ~ column1)

相关 - Convert string to a variable name