如何将具有逗号分隔值的因子转换为R中的数字向量?
我从json文件导入数据,需要一些元素作为数值 json:如何将具有逗号分隔值的因子转换为R中的数字向量?,r,json,import,numeric,R,Json,Import,Numeric,我从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" } ]
[
{
"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))
致意
tholori
help(“strsplit”)
您可以尝试as.numeric(as.character(json\u文件$d))
help(“strsplit”)
您可以尝试as.numeric(as.character(json\u文件$d))
。
d_numeric <- as.numeric(json_file$d)
str(json_file$d)
Factor w/ 1 level "1,2,3,4,5,6,7,8,9": 1
- attr(*, "names")= chr "Woche"