查找列中是否有多个 类 data.table
finding if a column has multiple classes in it data.table
我有一个巨大的 data.table,所以我无法在列中看到我的所有条目。
我想将一个显然是 class 字符的列转换为数字,但是,当我使用 as.numeric(col_name) 时,我收到警告 "NAs introduced by coercion".在我做任何其他事情之前,我想知道我是否可以找出列中的哪些条目不是字符或导致问题的原因。
我在 data.table 上做 str 得到:
Classes ‘data.table’ and 'data.frame': 57042881 obs. of 21 variables:
$ V1 : int 142466 1265 142510 199933 143297 13548 143605 15194 143894 16701 ...
$ V2 : int 1 1 1 1 1 1 1 1 1 1 ...
$ V3 : int 20150702 20160316 20150702 20160316 20150703 20160324 20150704 20160327 20150704 20160331 ...
$ V4 : int 14 17 15 6 16 17 9 20 14 15 ...
$ V5 : chr "2015-07-02 14:50:00" "2016-03-16 17:40:00" "2015-07-02 15:58:00" "2016-03-16 06:20:00" ...
$ V6 : int 33547 25523 33547 25523 33547 25523 33547 25523 33547 25523 ...
$ V7 : num 42.9 33.9 53.8 65.3 35.7 ...
$ V8 : int 2 2 2 2 2 2 2 2 2 2 ...
$ V9 : num 60 34.5 75.3 66.5 50 ...
$ V10: num 5.46 3.14 6.84 6.05 4.55 3.3 0.71 2.18 3.11 1.82 ...
$ V11: chr "1.271732" "0.926145" "1.271883" "0.926295" ...
$ V12: num 1.4 1.02 1.4 1.02 1.4 ...
$ V13: int 0 0 0 0 0 0 0 0 0 0 ...
$ V14: int 0 0 0 0 0 1 0 0 0 0 ...
$ V16: chr "ULP" "ULP" "ULP" "ULP" ...
$ V17: POSIXct, format: "2015-07-02 14:50:00" "2016-03-16 17:40:00" "2015-07-02 15:58:00" "2016-03-16 06:20:00" ...
$ V18: Date, format: "2015-07-02" "2016-03-16" "2015-07-02" "2016-03-16" ...
$ V19: int 2015 2016 2015 2016 2015 2016 2015 2016 2015 2016 ...
$ V20: int 7 3 7 3 7 3 7 3 7 3 ...
$ V21: int 2 16 2 16 3 24 4 27 4 31 ...
然后我想将 V11 转换为数字。
dt_2 <- dt[, V11 := as.numeric(V11)]
Warning message:
In eval(expr, envir, enclos) : NAs introduced by coercion
为什么我会收到此警告?是不是因为V11列里面有character以外的类型?如果是这样,我如何找到 V11 列中不是字符的值?
谢谢!
由于数据集非常大,最好在新会话中再次阅读单列 (因为 OP 已经将列 'V11' 替换为将 (:=
) 分配给自己。
library(data.table)
dt1 <- fread("yourfile.csv", select = 11)
通过使用select
参数,我们可以读取特定的列。然后,我们将该列转换为 numeric
,用 is.na
检查 NA 元素也创建一个逻辑 vector
.
i1 <- is.na(as.numeric(dt1[[1]]))
根据 'i1'
对列进行子集化
v1 <- dt1[[1]][i1]
然后做调查
1 小时后
根据调查,OP 提到这些值为 "null"
。在那种情况下,我们可以在 fread
中使用 na.strings = "null"
并且它应该用 NA
替换 "null" 并且我们得到正确的 class
(假设没有其他非数字字符串)
dt2 <- fread("yourfile.csv", na.strings = "null")
我有一个巨大的 data.table,所以我无法在列中看到我的所有条目。
我想将一个显然是 class 字符的列转换为数字,但是,当我使用 as.numeric(col_name) 时,我收到警告 "NAs introduced by coercion".在我做任何其他事情之前,我想知道我是否可以找出列中的哪些条目不是字符或导致问题的原因。
我在 data.table 上做 str 得到:
Classes ‘data.table’ and 'data.frame': 57042881 obs. of 21 variables:
$ V1 : int 142466 1265 142510 199933 143297 13548 143605 15194 143894 16701 ...
$ V2 : int 1 1 1 1 1 1 1 1 1 1 ...
$ V3 : int 20150702 20160316 20150702 20160316 20150703 20160324 20150704 20160327 20150704 20160331 ...
$ V4 : int 14 17 15 6 16 17 9 20 14 15 ...
$ V5 : chr "2015-07-02 14:50:00" "2016-03-16 17:40:00" "2015-07-02 15:58:00" "2016-03-16 06:20:00" ...
$ V6 : int 33547 25523 33547 25523 33547 25523 33547 25523 33547 25523 ...
$ V7 : num 42.9 33.9 53.8 65.3 35.7 ...
$ V8 : int 2 2 2 2 2 2 2 2 2 2 ...
$ V9 : num 60 34.5 75.3 66.5 50 ...
$ V10: num 5.46 3.14 6.84 6.05 4.55 3.3 0.71 2.18 3.11 1.82 ...
$ V11: chr "1.271732" "0.926145" "1.271883" "0.926295" ...
$ V12: num 1.4 1.02 1.4 1.02 1.4 ...
$ V13: int 0 0 0 0 0 0 0 0 0 0 ...
$ V14: int 0 0 0 0 0 1 0 0 0 0 ...
$ V16: chr "ULP" "ULP" "ULP" "ULP" ...
$ V17: POSIXct, format: "2015-07-02 14:50:00" "2016-03-16 17:40:00" "2015-07-02 15:58:00" "2016-03-16 06:20:00" ...
$ V18: Date, format: "2015-07-02" "2016-03-16" "2015-07-02" "2016-03-16" ...
$ V19: int 2015 2016 2015 2016 2015 2016 2015 2016 2015 2016 ...
$ V20: int 7 3 7 3 7 3 7 3 7 3 ...
$ V21: int 2 16 2 16 3 24 4 27 4 31 ...
然后我想将 V11 转换为数字。
dt_2 <- dt[, V11 := as.numeric(V11)]
Warning message:
In eval(expr, envir, enclos) : NAs introduced by coercion
为什么我会收到此警告?是不是因为V11列里面有character以外的类型?如果是这样,我如何找到 V11 列中不是字符的值?
谢谢!
由于数据集非常大,最好在新会话中再次阅读单列 (因为 OP 已经将列 'V11' 替换为将 (:=
) 分配给自己。
library(data.table)
dt1 <- fread("yourfile.csv", select = 11)
通过使用select
参数,我们可以读取特定的列。然后,我们将该列转换为 numeric
,用 is.na
检查 NA 元素也创建一个逻辑 vector
.
i1 <- is.na(as.numeric(dt1[[1]]))
根据 'i1'
对列进行子集化v1 <- dt1[[1]][i1]
然后做调查
1 小时后
根据调查,OP 提到这些值为 "null"
。在那种情况下,我们可以在 fread
中使用 na.strings = "null"
并且它应该用 NA
替换 "null" 并且我们得到正确的 class
(假设没有其他非数字字符串)
dt2 <- fread("yourfile.csv", na.strings = "null")