[英]How do I replace zeroes in multiple variable dataframes within a for loop?
So I have 1,000 dataframes to process using a FOR loop in R, say they are named DF1 to DF1000. 因此,我有1,000个数据帧要使用R中的FOR循环进行处理,说它们被命名为DF1至DF1000。 I need to replace all 0 values within the data frames to 1's.
我需要将数据帧中的所有0值替换为1。 I try to write this as follows.
我尝试这样写。
for (i in 1:1000){
eval(as.symbol(paste0("DF","i")))[eval(as.symbol(paste0("DF","i")))==0]<-1
}
This brings up an error ("could not find eval<-"). 这将引发错误(“找不到eval <-”)。 How do I resolve this issue?
我该如何解决这个问题? I tried using "assign" but get a different error ("target of assignment expands to non-language object").
我尝试使用“赋值”,但遇到了另一个错误(“赋值目标扩展为非语言对象”)。
Thanks in advance, Alvo 在此先感谢Alvo
One way is to assign
every character value DF
to a new DF
in the global environment, using base replace
function. 一种方法是使用基本
replace
功能assign
每个字符值DF
assign
全局环境中的新DF
。
for (i in 1:1000){
assign(paste0("DF", i),
replace(eval(parse(text = paste0("DF", i))), eval(parse(text = paste0("DF", i))) == 0, 1),
envir = .GlobalEnv)
}
This is similar to replace(DF,DF == 0, 1)
outside the for
loop. 这类似于
for
循环外的replace(DF,DF == 0, 1)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.