r - (函数(..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : Arguments imply different number of rows: 1, 4, 5, 2

标签 r dataframe

我是 R 的初学者。我希望你能帮助我解决我的问题。 我的数据集中的文件名包含大量信息。我必须提取这些信息来创建单独的变量。

首先我使用

splits <- t(as.data.frame(strsplit(as.character(rawdata_r$File),"_")))

但是当我使用它时,我收到此错误:

Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : Arguments imply different number of rows: 1, 4, 5, 2

可能是什么问题?感谢您提前提供的帮助。

最佳答案

您的错误是由 as.data.frame() 函数引发的。 R 中的数据帧必须具有相同行数的列。

给出错误消息:strsplit(as.character(rawdata_r$File),"_") 生成了一个包含 1、4、5 和 2 个嵌套元素的列表。这表明 rawdata_r$File 是您要转换为字符的一个因素。字符向量的长度为4,元素分别有0、3、4、1个“_”。也许这些是snake_case中的单词

根据您想要使用此对象的目的,我建议删除对 data.frame 的调用和对 t 的调用。如果您想使用蛇形命名约定将文件名转换为单词

请参阅以下示例:

# create an object with similar characteristics
filenames <- factor(c("foo", "foo_bar_baz_fiz", "foo_bar_baz_fiz_buz", "hello_world"))

# generate the error:
splits <- t(as.data.frame(strsplit(as.character(filenames),"_")))

Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : arguments imply differing number of rows: 1, 4, 5, 2

# don't generate the error
splits <- strsplit(as.character(filenames), "_")
splits
[[1]]
[1] "foo"

[[2]]
[1] "foo" "bar" "baz" "fiz"

[[3]]
[1] "foo" "bar" "baz" "fiz" "buz"

[[4]]
[1] "hello" "world"

关于r - (函数(..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : Arguments imply different number of rows: 1, 4, 5, 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49541205/

相关文章:

python - 如何比较 Pandas 中的时间频率?

python - 将 np.tril 和 np.triu 堆叠在一起

r - 当变量名称包含字符串信息时使用模式融化 - 避免强制转换为数字

r - slider 输入动态更新动画间隔

r - xgb.DMatrix 错误 : The length of labels must equal to the number of rows in the input data

r - 将 R 对象传递给 Rust 程序需要哪些步骤?

python - 如何根据DataFrame中下一周期的观察来过滤观察

R: "Scanning"小数点数字

python - 通过 Column Pandas 获取计数

python - pandas如何同时计算 bool 列值和其他列的不同计数