在R数据帧中循环行?

在R数据帧中循环行?,r,loops,for-loop,dataframe,R,Loops,For Loop,Dataframe,我在R中处理多个大数据帧,并尝试编写可以修改每个帧的函数(给定一组公共参数)。一个功能给我带来了麻烦(如下所示) 错误状态为“参数长度为零”,我认为这通常与向量中的空值相关。但是,我在给我错误的整个数据帧上使用了is.null,它确认DF中没有null值。我肯定我错过了一些相对简单的东西。任何建议都将不胜感激。错误来自线路 if(grep(".DERIVED", x[i,]) >= 1) 当grep没有找到术语“.DERIVED”时,它返回长度为零的内容,您的不等式不返回TRUE或FAL

我在R中处理多个大数据帧,并尝试编写可以修改每个帧的函数(给定一组公共参数)。一个功能给我带来了麻烦(如下所示)


错误状态为“参数长度为零”,我认为这通常与向量中的空值相关。但是,我在给我错误的整个数据帧上使用了is.null,它确认DF中没有null值。我肯定我错过了一些相对简单的东西。任何建议都将不胜感激。

错误来自线路

if(grep(".DERIVED", x[i,]) >= 1)
当grep没有找到术语“.DERIVED”时,它返回长度为零的内容,您的不等式不返回TRUE或FALSE,而是返回
逻辑(0)
。错误告诉您,
if
语句无法评估
logical(0)>=1

一个简单的例子:

if(grep(".DERIVED", "1234.DERIVEDabcdefg") >= 1) {print("it works")} # Works nicely, since the inequality can be evaluated
if(grep(".DERIVED", "1234abcdefg") > 1) {print("no dice")}
如果(长度(grep(“.DERIVED”,x[i,])!=0),您可以用
替换该行。


还有一点您还没有注意到,那就是您正在删除循环中的行/列。假设删除第5列,下一个循环迭代(当i=6时)将处理第7行!(这将在
[.data.frame
(x,i):选择未定义的列
)的
错误中结束)

如果您可以使用非base-R函数,这应该可以解决您的问题。
df
是这里讨论的
数据.frame
。它也比在行上循环更快(如果可以避免,一般不建议)

您可以将其作为一个函数,就像其他任何函数一样:

mattsFunction <- function(dat){
  dat %>%
    filter_all(!str_detect(., '\\.DERIVED')) %>%
    select_if(is.numeric)
}
mattsFunction%
筛选所有(!str\u detect(,'\\.DERIVED'))%>%
如果(为数值型),请选择
}

您可能应该给它起个更好的名字,尽管我更喜欢使用dplyr,但是如果您需要使用base R函数,有一些方法可以在不使用if语句的情况下实现这一点

注意,您应该考虑使用“EXECX版本<代码>”派生的“而不是<代码>”。派生“< /代码>,这意味着”任何字符都是派生的“.< /P>”。 我没有示例数据或输出,所以这里是我的最佳选择

# Made up data
test <- data.frame(a = c("data","data.DERIVED","data","data","data.DERIVED"),
                   b = (c(1,2,3,4,5)),
                   c = c("A","B","C","D","E"),
                   d = c(2,5,6,8,9),
                   stringsAsFactors = FALSE)

# Note: The following code assumes that the column class is numeric because the
# example code provided assumed that the column class was numeric. This will not 
# detects if the column is full of a string of character values of only numbers.

# Using the base subset command
test2 <- subset(test,
                subset = !grepl("\\.DERIVED",test$a),
                select = sapply(test,is.numeric))

# > test2
#   b d
# 1 1 2
# 3 3 6
# 4 4 8


# Trying to use []. Note: If only 1 column is numeric this will return a vector
# instead of a data.frame
test2 <- test[!grepl("\\.DERIVED",test$a),]
test2 <- test2[,sapply(test,is.numeric)]

# > test2
#   b d
# 1 1 2
# 3 3 6
# 4 4 8
#合成数据

测试时要小心删除
for循环
中的行,在您的第一个
for
if
语句中,您将删除
x
的行,然后在原始行的索引上循环,这将是一个无效的索引。尽可能共享示例数据(以及您想要的结果)也很有帮助长度修正有效!谢谢。你也发现了索引错误,哈哈。有没有办法解决这个问题,或者使用循环来删除数据帧中的向量是个坏主意?@MattGreenig我肯定会寻找另一种方法。有没有办法把它变成一个函数,这样我就可以轻松地在多个数据帧上调用它?是的,就像y一样你可以做其他任何事情。我编辑了这篇文章来创建一个函数。
library(dplyr)
library(stringr)

df %>%
  filter_all(!str_detect(., '\\.DERIVED')) %>%
  select_if(is.numeric)
mattsFunction <- function(dat){
  dat %>%
    filter_all(!str_detect(., '\\.DERIVED')) %>%
    select_if(is.numeric)
}
# Made up data
test <- data.frame(a = c("data","data.DERIVED","data","data","data.DERIVED"),
                   b = (c(1,2,3,4,5)),
                   c = c("A","B","C","D","E"),
                   d = c(2,5,6,8,9),
                   stringsAsFactors = FALSE)

# Note: The following code assumes that the column class is numeric because the
# example code provided assumed that the column class was numeric. This will not 
# detects if the column is full of a string of character values of only numbers.

# Using the base subset command
test2 <- subset(test,
                subset = !grepl("\\.DERIVED",test$a),
                select = sapply(test,is.numeric))

# > test2
#   b d
# 1 1 2
# 3 3 6
# 4 4 8


# Trying to use []. Note: If only 1 column is numeric this will return a vector
# instead of a data.frame
test2 <- test[!grepl("\\.DERIVED",test$a),]
test2 <- test2[,sapply(test,is.numeric)]

# > test2
#   b d
# 1 1 2
# 3 3 6
# 4 4 8