修改并重新创建R中的data.frame列表 [英] Modify and recreate a list of data.frame in R

查看:193
本文介绍了修改并重新创建R中的data.frame列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为mdata.framelist(请参阅

I have a list of data.frames called m (see HERE). Column r in these data.frames is all NA.

但是后来,我计算了其中一些r,并将它们存储为名为L的列表.

But later on, I have computed some of these rs and stored them as a list called L.

我想知道如何实现以下目标?

(1)如果L中的任何列表条目(即L[[1]]L[[2]],...)以紧随NA之后的数字开头,则用该数字替换NA

(1) If any list entry in L (i.e., L[[1]], L[[2]], ...), starts with a number BUT right after it is NA, replace NA with that number.

(2)将所有新的r(存储在L中)放回原始数据列表m中的列r中.

(2) Put back all new rs (stored in L) in column r, in the original list of data.frames m.

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? ######

推荐答案

如果要用相应的"L"替换"m"中data.framelistlist中的"r"列的vectorsna.locf0一起应用,然后

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

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

这篇关于修改并重新创建R中的data.frame列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆