【发布时间】:2016-05-30 02:36:55
【问题描述】:
我想将我的数据 train.user(213451 个变量,共 20 个变量。其中 2 个变量是列表)保存为 csv 文件。
我用:
write.csv(train.user, "train_user.csv", row.names = FALSE)
但发生错误
Error in .External2(C_writetable, x, file, nrow(x), p, rnames, sep, eol, :
unimplemented type 'list' in 'EncodeElement'
这就是我的数据train.user 的样子。 (通过使用str)(仅显示部分内容)
'data.frame': 213451 obs. of 20 variables:
$ id : Factor w/ 213451 levels "00023iyk9l","0005ytdols",..: 100523 48039 26485 68504 48956 147281 129610 2144 59779 40826 ...
$ gender : Factor w/ 4 levels "-unknown-","FEMALE",..: 1 3 2 2 1 1 2 2 2 1 ...
$ age :List of 213451
..$ : num NA
..$ : num 38
..$ : num 56
..$ : num 42
..$ : num 41
.. [list output truncated]
age 列似乎存储为列表,write.csv 不接受这种格式。根据我天真的直觉,我尝试使用以下代码将该列重新存储为数据框,但失败了。
train.user$age <- as.data.frame(train.user$age)
错误信息:
Error in `$<-.data.frame`(`*tmp*`, "age", value = list(NA_real_. = NA_real_, :
replacement has 1 row, data has 213451
我也按照另一篇帖子的建议尝试了train.user$age <- data.frame(lapply(train.user$age, unlist)),但出现了同样的错误。
感谢您的帮助!
【问题讨论】:
-
没有数据我不能自己尝试任何东西,但是使用
train.user$age <- unlist(train.user$age)呢?您不想将其存储回data.frame。 -
请提供样本数据。只有这样,才有可能针对您的案例制定解决方案。
-
感谢@JonathanCarroll,您提供的代码有效!稍后我将在帖子中为有相同问题的人进行总结。但是,我仍然想知道使用
unlist背后的逻辑是什么(以及为什么不接受列表?)。如果您能对此进行更多解释,那就太好了。