我有以下格式的数据框df
:
v2 v3
2.3 c(1,5,8,2)
1.2 c(2,4,3,2)
typeof(df$v3[1])
是list
,我想将其转换为矢量。所以我写了一个`sapply``函数并运行它:
df$v3 <- sapply(
df$v3,
function(x) {x <- unlist(x)}
)
但它只是继续运行而不会产生任何结果。我也试过lapply
,但它无法给我预期的结果。相反,它再次生成一个列表。
我的dput(droplevels(head(df)))
结果是:
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0))), .Names = c("V2", "V3"), row.names = c(NA,
-6L), .internal.selfref = <pointer: 0x102010578>, class = c("data.table", "data.frame"))
你能告诉我如何解决它吗?
编辑:
我运行df$V3 <- unlist(df$V3)
很长一段时间。但类型仍然是列表,并生成警告:
Warning messages:
1: In `[<-.data.table`(x, j = name, value = value) :
Supplied 496450000 items to be assigned to 99290 items of column 'V3' (496350710 unused)
2: In `[<-.data.table`(x, j = name, value = value) :
Coerced 'double' RHS to 'list' to match the column's type; may have truncated precision. Either change the target column to 'double' first (by creating a new 'double' vector length 99290 (nrows of entire table) and assign that; i.e. 'replace' column), or coerce RHS to 'list' (e.g. 1L, NA_[real|integer]_, as.*, etc) to make your intent clear and for speed. Or, set the column type correctly up front when you create the table and stick to it, please.