Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.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_Integer_Multiple Columns - Fatal编程技术网

R 如何对列表中所有数据帧的列进行复杂的编辑?

R 如何对列表中所有数据帧的列进行复杂的编辑?,r,list,dataframe,integer,multiple-columns,R,List,Dataframe,Integer,Multiple Columns,我有一个185个数据帧的列表,称为WaFramesNumeric。每个数据帧有几百列和数千行。我希望编辑每个数据帧,以便它保留我指定的所有数字列以及任何非数字列 使用: for(i in seq_along(WaFramesNumeric)) { WaFramesNumeric[[i]] <- WaFramesNumeric[[i]][,sapply(WaFramesNumeric[[i]],is.numeric)] } 成功使每个数据帧仅包含其数值列 我试着用行来修改它,以添

我有一个185个数据帧的列表,称为WaFramesNumeric。每个数据帧有几百列和数千行。我希望编辑每个数据帧,以便它保留我指定的所有数字列以及任何非数字列

使用:

for(i in seq_along(WaFramesNumeric)) {
    WaFramesNumeric[[i]] <- WaFramesNumeric[[i]][,sapply(WaFramesNumeric[[i]],is.numeric)] 
}
成功使每个数据帧仅包含其数值列

我试着用行来修改它,以添加特定的列。我试过:

for (i in seq_along(WaFramesNumeric)) {
    a <- WaFramesNumeric[[i]]$Device_Name
    WaFramesNumeric[[i]] <- WaFramesNumeric[[i]][,sapply(WaFramesNumeric[[i]],is.numeric)] 
    cbind(WaFramesNumeric[[i]],a)
}
并尝试调用所有整数列以及特定列的列号,然后基于此进行组合:

for (i in seq_along(WaFramesNumeric)) {
    f <- which(sapply(WaFramesNumeric[[i]],is.numeric))
    m <- match("Cost_Center",colnames(WaFramesNumeric[[i]]))
    n <- match("Device_Name",colnames(WaFramesNumeric[[i]]))
    combine <- c(f,m,n)
    WaFramesNumeric[[i]][,i,combine]
}

这些都会返回错误,我很困惑如何才能做到这一点。WaFramesNumeric是另一个数据帧列表的副本WaFramesNumeric您错误地认为for循环中的最后一个命令是有意义的。事实并非如此。事实上,它正在被丢弃,因此,由于您从未将它分配给任何位置cbind和WaFramesNumeric…的索引,它将被默默地丢弃

此外,您在第三个代码块中过度索引了data.frame。首先,它在data.frame中使用i,即使i是data.frames列表中的索引,而不是帧本身。第二个原因可能是,您正在尝试索引二维帧的三维。只需将最后一个索引从[,i,combine]更改为[,combine]或[combine]

第三个问题,虽然可能还没有看到,是比赛将返回NA,如果没有发现。使用NA索引帧会返回一个错误,请尝试mtcars[,NA]查看。我建议您可以用grep替换match:当找不到任何内容时,它将返回integer0,这就是您在本例中想要的

for (i in seq_along(WaFramesNumeric)) {
  f <- which(sapply(WaFramesNumeric[[i]], is.numeric))
  m <- grep("Cost_Center", colnames(WaFramesNumeric[[i]]))
  n <- grep("Device_Name", colnames(WaFramesNumeric[[i]]))
  combine <- c(f,m,n)
  WaFramesNumeric[[i]] <- WaFramesNumeric[[i]][combine]
}

您错误地认为for循环中的最后一个命令是有意义的。事实并非如此。事实上,它正在被丢弃,因此,由于您从未将它分配给任何位置cbind和WaFramesNumeric…的索引,它将被默默地丢弃

此外,您在第三个代码块中过度索引了data.frame。首先,它在data.frame中使用i,即使i是data.frames列表中的索引,而不是帧本身。第二个原因可能是,您正在尝试索引二维帧的三维。只需将最后一个索引从[,i,combine]更改为[,combine]或[combine]

第三个问题,虽然可能还没有看到,是比赛将返回NA,如果没有发现。使用NA索引帧会返回一个错误,请尝试mtcars[,NA]查看。我建议您可以用grep替换match:当找不到任何内容时,它将返回integer0,这就是您在本例中想要的

for (i in seq_along(WaFramesNumeric)) {
  f <- which(sapply(WaFramesNumeric[[i]], is.numeric))
  m <- grep("Cost_Center", colnames(WaFramesNumeric[[i]]))
  n <- grep("Device_Name", colnames(WaFramesNumeric[[i]]))
  combine <- c(f,m,n)
  WaFramesNumeric[[i]] <- WaFramesNumeric[[i]][combine]
}

我不确定您试图调用所有整数列的列号是什么意思…,但如果您想查看数据帧列表,并根据某个函数选择一些列并保持给定的列名,您可以这样做:

df <- data.frame(a=rnorm(20), b=rnorm(20), c=letters[1:20], d=letters[1:20], stringsAsFactors = FALSE)
WaFramesNumeric <- rep(list(df), 2)

Selector <- function(data, select_func, select_names) {
  select_func <- match.fun(select_func)
  idx_names <- match(select_names, colnames(data))
  idx_names <- idx_names[!is.na(idx_names)]
  idx_func <- which(sapply(data, select_func))
  idx <- unique(c(idx_func, idx_names))
  return(data[, idx])
}

res <- lapply(X = WaFramesNumeric, FUN = Selector, select_names=c("c"), select_func = is.numeric)

我不确定您试图调用所有整数列的列号是什么意思…,但如果您想查看数据帧列表,并根据某个函数选择一些列并保持给定的列名,您可以这样做:

df <- data.frame(a=rnorm(20), b=rnorm(20), c=letters[1:20], d=letters[1:20], stringsAsFactors = FALSE)
WaFramesNumeric <- rep(list(df), 2)

Selector <- function(data, select_func, select_names) {
  select_func <- match.fun(select_func)
  idx_names <- match(select_names, colnames(data))
  idx_names <- idx_names[!is.na(idx_names)]
  idx_func <- which(sapply(data, select_func))
  idx <- unique(c(idx_func, idx_names))
  return(data[, idx])
}

res <- lapply(X = WaFramesNumeric, FUN = Selector, select_names=c("c"), select_func = is.numeric)

您对WaFramesNumeric[[i]][,i,combine]的双逗号索引有何期待?据我所知,使用combine填充的位置仅用于drop=或exact=,而不是整数位置。另外,如果我在列表中引用了特定的data.frame,为什么要在内部data.frame索引中使用它?我认为它只是华夫拉美尔[[I]][,combine]。@r2evans说实话,我是R的新手,我已经在这个特定问题上工作了很长时间。当我收到错误时,我没有指定列。我确实犯了一些错误,但我只是想在提问时展示我的思维过程。理想情况下,第一行可以添加几行,以指定将列添加到仅为数字列的新框架中。使用WaFramesNumeric[[i]][,i,combine]的双逗号索引,您期望得到什么?据我所知,使用combine填充的位置仅用于drop=或exact=,而不是整数位置。另外,如果我在列表中引用了特定的data.frame,为什么要在内部data.frame索引中使用它?我认为它只是华夫拉美尔[[I]][,combine]。@r2evans说实话,我是R的新手,我已经在这个特定问题上工作了很长时间。当我收到错误时,我没有指定列。我确实犯了一些错误,但我只是想在提问时展示我的思维过程。理想情况下,第一行可以添加几行,以指定将列添加到仅为数字列的新框架中。感谢您的澄清。我确实认为循环中的所有事情都是按顺序发生的,但我根本没有意识到这一点正在被丢弃。我运行了这个,但结果与我在seq_alongwaframesumeric{waframesumeric[[I]]编辑中的fori行相同:我尝试了不同的方法,但grep和你的exa
MPE是积分的,再次感谢!它很有魅力。谢谢你的澄清。我确实认为循环中的所有事情都是按顺序发生的,但我根本没有意识到这一点正在被丢弃。我运行了这个,但结果与我在seq_alongwaframesumeric{waframesumeric[[I]]编辑:我尝试了另一种方法,但是grep和你的例子是完整的,所以,再次感谢你!它像一个符咒一样工作。很抱歉不清楚。我试图通过循环,调用数据框中任何数字列和我指定的任何其他列的列号来解决我的问题。然后我要替换fram通过使用列号作为参考来绑定。但是谢谢你,它确实有帮助!我会尝试一下。很抱歉,不清楚。我试图通过循环调用数据帧的列号来解决我的问题,对于任何数字列和我指定的任何其他列。然后,我将通过绑定u来替换该帧唱列号作为参考。但谢谢你,它确实有帮助!我会尝试一下。