简体   繁体   English

修改并重新创建R中的data.frame列表

[英]Modify and recreate a list of data.frame in R

I have a list of data.frame s called m (see HERE ). 我有一个名为mdata.frame list (请参见HERE )。 Column r in these data.frames is all NA . 这些data.frames中的列r均为NA

But later on, I have computed some of these r s and stored them as a list called L . 但是稍后,我计算了其中的一些r并将它们存储为名为L的列表。

I'm wondering how to achieve the following?: 我想知道如何实现以下目标?:

(1) If any list entry in L (ie, L[[1]] , L[[2]] , ...), starts with a number BUT right after it is NA , replace NA with that number. (1)如果L任何列表条目(即L[[1]]L[[2]] ...)都以数字BUT开头,紧随其后是NA ,用该数字替换NA

(2) Put back all new r s (stored in L ) in column r , in the original list of data.frames m . (2)将所有新的r s(存储在L )放回data.frames m的原始列表中的r列中。

D <- read.csv("https://raw.githubusercontent.com/izeh/m/master/g.csv", h = T) ## Data


m <- split(D, D$study.name) ;  m[[1]] <- NULL  ## original list of data.frame    
                                               ## To be finally recreated.


 L <- list(Bit.KnoA = rep(NA, 8), Bit.KnoB = rep(NA, 12), ChandlerA = c(.5, .5), 
Mubarak = c(.6, NA, .5, NA, .5, NA, .8, NA, .5,NA,.9,NA), SheenA = rep(NA, 6),
Shin.Ellis = rep(NA, 6), Sun = rep(NA, 6), Trus.Hsu = rep(NA, 2))


lapply(L, transform, r = zoo::na.locf0(r)) ## To achieve (1), but Not working !


###### NOW, put back L in the new list of data.frame like `m` above? ######

If the intention is to replace the 'r' column in the list of data.frame in 'm' with the corresponding 'L' list of vectors applied with na.locf0 , then 如果打算将data.frame中的data.frame list中的r列替换为使用na.locf0vectors的L list ,则

library(zoo)
m1 <- Map(function(x, y) transform(x, r = na.locf0(y)), m, L)

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

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