修改并重新创建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). Column r
in these data.frames is all NA
.
但是后来,我计算了其中一些r
,并将它们存储为名为L
的列表.
But later on, I have computed some of these r
s 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 r
s (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.frame
的list
中list
中的"r"列的vectors
与na.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屋!