r - 如何将具有逗号分隔值的因子转换为 R 中的数字向量?
问题描述
我从 json 文件导入数据,我需要一些元素作为数值。
json:
[
{
"a": 52,
"b": 2020,
"c": 14,
"d": "1,2,3,4,5,6,7,8,9",
"e": "4,1,2,3,1,0,0,0,0",
"f": "TRUE"
}
]
导入代码:
json_file <- fromJSON(file = "file")
json_file <- lapply(json_file, function(x) {
x[sapply(x, is.null)] <- NA
unlist(x)
})
json_file <-as.data.frame(do.call("rbind", json_file))
现在我要做的是从 d 中获取值并将它们转换为数字向量:
d_numeric <- as.numeric(json_file$d)
发生的错误是“强制引入的 NA”,问题似乎是 d 中的单个值由于逗号(下面的 str)而被视为一个级别。我怎样才能把它们分开?
str(json_file$d)
Factor w/ 1 level "1,2,3,4,5,6,7,8,9": 1
- attr(*, "names")= chr "Woche"
最好的问候索洛里
解决方案
推荐阅读
- vim - 退出less后如何清屏,man和vim
- elasticsearch - 在 elasticsearch 中从远程和本地集群重新索引
- arrays - 如何根据 Google 表格中的布尔值将行合并为一列?
- string - 用于查找最长子字符串而不重复字符的代码不起作用
- mysql - 如何为每个唯一 ID 获取具有 MAX(field) 的行的数据
- c - 在其自己的 WindowProc 函数中调整控件大小的正确方法是什么?
- reactjs - React 子组件之间的交互:refs vs callback as props
- nginx - Nginx Ingress、HTTPS 和内部 HTTPs
- angular - Angular8 路由器不适用于某些组件 - 退回到索引
- ios - CircleCI 的 Fastlane 测试飞行