Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中更改数据帧列表中的多个列_R_List_Dataframe_Lapply - Fatal编程技术网

在R中更改数据帧列表中的多个列

在R中更改数据帧列表中的多个列,r,list,dataframe,lapply,R,List,Dataframe,Lapply,我有一个12个数据帧的列表。它们都有相同的结构。 其中一个看起来很奇怪: (每个数据帧中有9个,此处不能显示最后一个) 我构建了一个numburs向量,这样我就可以在每个数据帧中更改我的7列(day_mean列),这样它就可以得到NA的新值。所以我用这个函数来应用它: aa = seq(1, 168 , 24) # the numeric vector bb = rep(T, 168) bb[aa] = FALSE cc= (which(bb)) function(X) { # the fun

我有一个12个数据帧的列表。它们都有相同的结构。 其中一个看起来很奇怪: (每个数据帧中有9个,此处不能显示最后一个)

我构建了一个numburs向量,这样我就可以在每个数据帧中更改我的7列(day_mean列),这样它就可以得到NA的新值。所以我用这个函数来应用它:

aa = seq(1, 168 , 24) # the numeric vector
bb = rep(T, 168)
bb[aa] = FALSE
cc= (which(bb))

function(X) { # the function that cahnge my 7's column in each dataframe
X[,7][cc] = NA   

return(X)
}

lapply(kvish_1_10t.tables, func)
到目前为止一切都很好,但是当我尝试在多个列上应用相同的函数时,它会向我发送一个错误。我更改了函数,以便在这里写下时将其引用到其他列

function(X) { 
X[,7:9][cc] = NA

return(x)
{

 Error in `[<-.data.frame`(`*tmp*`, cc, value = NA) : 
 new columns would leave holes after existing columns 
函数(X){
X[,7:9][cc]=NA
返回(x)
{

“[如果您将
X[,7:9][cc]=NA
更改为
X[cc,7:9]=NA
,该怎么办?在我看来,您的“顺序”或列表子集似乎是错误的。第一个元素始终是列表的编号或名称。第二个元素是
[]
可以子集data.frame/matrix-如果此列表项是data.frame/matrix,我尝试了此操作,但它显示了一个错误:`error in
*tmp*
[[j]]:递归索引在级别2失败`
function(X) { 
X[,7:9][cc] = NA

return(x)
{

 Error in `[<-.data.frame`(`*tmp*`, cc, value = NA) : 
 new columns would leave holes after existing columns