首页 文章

R - 从因子到数字或整数错误

提问于
浏览
5

我在 R 中有一个从CSV文件加载的数据帧 . 其中一个变量称为"Amount",意味着包含正数和负数 .

当我查看数据帧时,此变量的数据类型被列为一个因子,我需要它以数字格式(不确定哪种 - 但整数 - 数字,嗯......?) . 所以,我试图将它转换为这两种格式中的一种,但看到了一些有趣的行为 .

初始数据帧:

str(df)

Amount        : Factor w/ 11837 levels "","-1","-10",..: 2 2 1664 4 6290 6290 6290 6290 6290 6290 ...

正如我上面提到的,当我尝试将其转换为数字或整数时,我看到了一些奇怪的东西 . 为了表明这一点,我把这个比较放在一起:

df2 <- data.frame(df$Amount, as.numeric(df$Amount), as.integer(df$Amount))

str(df2)
'data.frame':   2620276 obs. of  3 variables:
 $ df.Amount            : Factor w/ 11837 levels "","-1","-10",..: 2 2 1664 4 6290 6290 6290 6290 6290 6290 ...
 $ as.numeric.df.Amount.: num  2 2 1664 4 6290 ...
 $ as.integer.df.Amount.: int  2 2 1664 4 6290 6290 6290 6290 6290 6290 ...

> head(df2, 20)
         df.Amount        as.numeric.df.Amount.       as.integer.df.Amount.
1               -1                           2                           2
2               -1                           2                           2
3             -201                        1664                        1664
4             -100                           4                           4
5                1                        6290                        6290
6                1                        6290                        6290
7                1                        6290                        6290
8                1                        6290                        6290
9                1                        6290                        6290
10               1                        6290                        6290
11               1                        6290                        6290
12               1                        6290                        6290
13               1                        6290                        6290
14               1                        6290                        6290
15               1                        6290                        6290
16               1                        6290                        6290
17               1                        6290                        6290
18               2                        7520                        7520
19               2                        7520                        7520
20               2                        7520                        7520

as.numericas.integer 函数正在使用Amount变量并对其执行某些操作,但我不知道这是什么 . 我的目标是将Amount变量变为某种数字类型,这样我就可以对它执行sum / mean / etc .

我错误地做了什么导致奇怪的数字,我该怎么做才能解决它?

3 回答

  • 1

    问题的根源可能是您导入的csv中的一些时髦 Value . 如果它来自excel,这并不罕见 . 它可以是百分比符号,来自excel的“注释”字符或任何一长串事物 . 我会在您选择的编辑器中查看csv并查看您可以看到的内容 .

    除此之外,您还有一些选择 .

    read.csv 采用可选参数 stringsAsFactors ,您可以将其设置为 FALSE

    因子存储为映射到值的整数级别 . 当您使用 as.numeric 直接转换时,您将使用这些整数级别而不是初始值:

    > x<-10:20
    > as.numeric(factor(x))
     [1]  1  2  3  4  5  6  7  8  9 10 11
    >
    

    否则看 ?factor

    特别是,as.numeric应用于一个因子是没有意义的,并且可能通过隐式强制发生 . 要将因子f转换为大约其原始数值,建议使用as.numeric(levels(f))[f],并且比as.numeric(as.character(f))稍微更有效 .

    但是,我怀疑这会出错,因为输入除了数字之外还有其他内容 .

  • 10

    @Justin是对的 . 以下是如何查找违规值的演练:

    # A sample data set with a weird value ("4%") in it
    d <- read.table(text="A B\n1 2\n3 4%\n", header=TRUE)
    str(d)
    #'data.frame':   2 obs. of  2 variables:
    # $ A: int  1 3
    # $ B: Factor w/ 2 levels "2","4%": 1 2
    
    as.numeric(d$B) # WRONG, returns 1 2 (the internal factor codes)
    
    # This correctly converts to numeric
    x <- as.numeric(levels(d$B))[d$B] # 2 NA
    
    # ...and this finds the offending value(s):
    d$B[is.na(x)]  # 4% 
    
    # and this finds the offending row numbers:
    which(is.na(x)) # row 2
    

    请注意,如果您的数据集的缺失值编码为空单元格或字符串“NA”以外的其他值,则必须将其指定为read.table:

    # Here "N/A" is used instead of "NA"...
    read.table(text="A B\n1 2\n3 N/A\n", header=TRUE, na.strings="N/A")
    
  • 10

    我是新来的,但我一直在使用这个论坛来查询 . 我有类似的问题,但下面的工作对我来说 . 我正在将数据从txt文件移植到数据框

    data <- read.delim(paste(folderpath,"data.txt",sep=""),header=TRUE,sep="\\",as.is=6)
    

    请注意,我在第6列使用了as.is,其中包含数字数据以及某些行中的一些垃圾字符 . 使用as.is将数据作为第6列中的字符移植 . 然后,以下内容将第6列中的字符更改为数值 . 所有垃圾值都转换为NA,以后可以删除 .

    data[,6] <- as.numeric(data[,6])
    

    希望这可以帮助

相关问题