fread()将所有列类分配给指定的字符

时间:2016-08-05 16:32:31

标签: r data.table fread

我在下面添加了我的数据集。我尝试使用rfread中阅读它。但是列类都是character,即使我已经指定了它们。

class <- c("character", "character", "numeric", "numeric","numeric", 
"character", "numeric", "numeric", rep("chracter", 5), "numeric",                                    
"chracter", "character", "factor", "character",    
"character", "character", "character", "character", "factor",                                          
"numeric", "numeric", "character",rep("numeric", 6), "character",                                      
"numeric", "factor", rep("numeric", 9) , "character", "numeric",                                       
"character", "character", "numeric", "numeric", "numeric", "factor",                                   
"factor", "numeric", "numeric", "factor", rep("numeric", 55))

data_q1 <- fread("LoanStats_2016Q1.csv", header = TRUE, skip = 1, nrows = 133887,  colClasses = class, fill = TRUE)

str(data_q1)

Classes ‘data.table’ and 'data.frame':  133887 obs. of  111 variables:
 $ id                            : chr  "75577129" "75669195" "75769072" "75991583" ...
 $ member_id                     : chr  "81011841" "81136933" "81236807" "81482303" ...
 $ loan_amnt                     : chr  "25000" "4000" "3600" "8000" ...
 $ funded_amnt                   : chr  "25000" "4000" "3600" "8000" ...
 $ funded_amnt_inv               : chr  "25000" "4000" "3600" "8000" ...

我已查看此answer并尝试了此操作:

any(is.na(data_q1[, loan_amnt]))
[1] FALSE

我的loan_amnt列不包含NA个值。现在我不知道问题所在。

data

1 个答案:

答案 0 :(得分:1)

你在类向量中有拼写错误。 rep(“chracter”,5)应该是rep(“character”,5),这个错误在同一个向量中再次完成