如何确定字符向量是有效的数字还是整数向量 [英] how to determine if a character vector is a valid numeric or integer vector
问题描述
我正在尝试将嵌套列表结构转换为数据框.该列表看起来类似于以下内容(它是使用httr包从解析的JSON中读取的序列化数据).
I am trying to turn a nested list structure into a dataframe. The list looks similar to the following (it is serialized data from parsed JSON read in using the httr package).
myList <- list(object1 = list(w=1, x=list(y=0.1, z="cat")), object2 = list(w=NULL, x=list(z="dog")))
我的原始示例数据太简单了.实际数据参差不齐,这意味着并非每个对象都存在所有变量,并且某些列表元素为NULL.我编辑了数据以反映这一点.
unlist(myList)
在递归地展平列表方面做得很好,然后我可以使用lapply
很好地展平所有对象.
unlist(myList)
does a great job of recursively flattening the list, and I can then use lapply
to flatten all the objects nicely.
flatList <- lapply(myList, FUN= function(object) {return(as.data.frame(rbind(unlist(object))))})
最后,我可以使用plyr::rbind.fill
myDF <- do.call(plyr::rbind.fill, flatList)
str(myDF)
#'data.frame': 2 obs. of 3 variables:
#$ w : Factor w/ 2 levels "1","2": 1 2
#$ x.y: Factor w/ 2 levels "0.1","0.2": 1 2
#$ x.z: Factor w/ 2 levels "cat","dog": 1 2
问题在于w和x.y现在被解释为字符向量,默认情况下将其解析为数据帧中的因子.我相信unlist()
是罪魁祸首,但我想不出另一种方法来递归地平滑列表结构.一种解决方法是对数据框进行后处理,然后分配数据类型.确定向量是有效数字还是整数向量的最佳方法是什么?
The problem is that w and x.y are now being interpreted as character vectors, which by default get parsed as factors in the dataframe. I believe that unlist()
is the culprit, but I can't figure out another way to recursively flatten the list structure. A workaround would be to post-process the dataframe, and assign data types then. What is the best way to determine if a vector is a valid numeric or integer vector?
推荐答案
As discussed here, checking if as.numeric
returns NA
values is a simple approach to checking if a character string contains numeric data. Now you can do something like:
myDF2 <- lapply(myDF, function(col) {
if (suppressWarnings(all(!is.na(as.numeric(as.character(col)))))) {
as.numeric(as.character(col))
} else {
col
}
})
str(myDF2)
# List of 3
# $ w : num [1:2] 1 2
# $ x.y: num [1:2] 0.1 0.2
# $ x.z: Factor w/ 2 levels "cat","dog": 1 2
这篇关于如何确定字符向量是有效的数字还是整数向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!