简体   繁体   English

R替换多个数据帧的多个列中的值

[英]R replacing values from multiple columns of multiple data frames

I've been trying to figure that out for a whole day now, but my understanding of loops is just not the best. 我已经尝试了整整一天,但是我对循环的理解并不是最好的。 I basically have 3 data frames. 我基本上有3个数据帧。 They contain several columns. 它们包含几列。 I want to check each data frame at a time. 我想一次检查每个数据帧。 If one value in a column is -9999 (NA), I want to replace it with a value from the other data frames (if the same value from the 2nd data frame is -9999 too, it should take the value from the 3rd one). 如果一列中的一个值是-9999(NA),我想用其他数据帧中的值替换它(如果第二个数据帧中的相同值也是-9999,则应取第三个数据帧中的值)。

I figured it out with if else. 我想办法了。 I just can't get it into a for loop. 我就是无法进入for循环。 So I have to type each column by hand, which takes a lot of time, since I have many columns with weird names. 因此,我必须手动键入每一列,这会花费很多时间,因为我有许多带有奇怪名称的列。 So here's my example, in simple form. 所以这是我的示例,形式简单。 Maybe some can help me. 也许有些可以帮助我。 Thanks a lot, Susi 非常感谢Susi

par1 <- c(1,2,3,4)
par2 <- c(1,2,3,0)
par3 <- c(1,0,3,8)
par4 <- c(1,0,3,9)

r <- data.frame(par1, par2, par3, par4)
d <- data.frame(par1, par2, par3, par4)
b <- data.frame(par1, par2, par3, par4)

r$par1[4] <- -9999

gap_filling <- function(x,y,z){
  ifelse (x == -9999, 
          ifelse(y==-9999, z, y),
          x)
} ## this is the function I wrote to shorten it a little bit

r$par1 <- gap_filling(r$par1, d$par1, b$par1)
r$par2 <- gap_filling(r$par2, d$par2, b$par2)
r$par3 <- gap_filling(r$par3, d$par3, b$par3)
r$par4 <-gap_filling(r$par4, d$par4, b$par4)

### this is just the replacing for the first data frame. I need to do the same with the other two too

(edited version, thanks to Jake Burkead's comment) (编辑版,感谢杰克·伯克(Jake Burkead)的评论)

What you really want is the power of logical indexing. 您真正想要的是逻辑索引的功能。

For example: 例如:

r[r==-9999] <- d[r==-9999]
r[r==-9999] <- b[r==-9999]
# # Alternate writing, if you have 'NA' instead of -9999
# # r[is.na(r)] <- matrix.d[is.na(r)]
# # r[is.na(r)] <- matrix.b[is.na(r)]

You could organize r , b , d in a named list and proceed as @Jealie and @JakeBurkhead suggested: 您可以在命名list组织rbd ,然后按照@Jealie和@JakeBurkhead的建议进行操作:

l <- list(r=r, d=d, b=b) 
invisible(lapply(1:length(l), function(i) { 
  for (x in setdiff(1:length(l), i))      
       l[[i]] [ l[[i]] == -9999] <<- l[[x]] [ l[[i]] == -9999]
}))
invisible(list2env(l, envir=.GlobalEnv)) # overwrite existing r, d, b  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM