hurriedly% 2010-02-18 12:17 采纳率: 100%
浏览 2320
已采纳

如何将数据框列转换为数字类型?

How do you convert a data frame column to a numeric type?

转载于:https://stackoverflow.com/questions/2288485/how-to-convert-a-data-frame-column-to-numeric-type

  • 写回答

15条回答 默认 最新

  • elliott.david 2010-02-19 00:31
    关注

    Since (still) nobody got check-mark, I assume that you have some practical issue in mind, mostly because you haven't specified what type of vector you want to convert to numeric. I suggest that you should apply transform function in order to complete your task.

    Now I'm about to demonstrate certain "conversion anomaly":

    # create dummy data.frame
    d <- data.frame(char = letters[1:5], 
                    fake_char = as.character(1:5), 
                    fac = factor(1:5), 
                    char_fac = factor(letters[1:5]), 
                    num = 1:5, stringsAsFactors = FALSE)
    

    Let us have a glance at data.frame

    > d
      char fake_char fac char_fac num
    1    a         1   1        a   1
    2    b         2   2        b   2
    3    c         3   3        c   3
    4    d         4   4        d   4
    5    e         5   5        e   5
    

    and let us run:

    > sapply(d, mode)
           char   fake_char         fac    char_fac         num 
    "character" "character"   "numeric"   "numeric"   "numeric" 
    > sapply(d, class)
           char   fake_char         fac    char_fac         num 
    "character" "character"    "factor"    "factor"   "integer" 
    

    Now you probably ask yourself "Where's an anomaly?" Well, I've bumped into quite peculiar things in R, and this is not the most confounding thing, but it can confuse you, especially if you read this before rolling into bed.

    Here goes: first two columns are character. I've deliberately called 2nd one fake_char. Spot the similarity of this character variable with one that Dirk created in his reply. It's actually a numerical vector converted to character. 3rd and 4th column are factor, and the last one is "purely" numeric.

    If you utilize transform function, you can convert the fake_char into numeric, but not the char variable itself.

    > transform(d, char = as.numeric(char))
      char fake_char fac char_fac num
    1   NA         1   1        a   1
    2   NA         2   2        b   2
    3   NA         3   3        c   3
    4   NA         4   4        d   4
    5   NA         5   5        e   5
    Warning message:
    In eval(expr, envir, enclos) : NAs introduced by coercion
    

    but if you do same thing on fake_char and char_fac, you'll be lucky, and get away with no NA's:

    > transform(d, fake_char = as.numeric(fake_char), 
                   char_fac = as.numeric(char_fac))
    
      char fake_char fac char_fac num
    1    a         1   1        1   1
    2    b         2   2        2   2
    3    c         3   3        3   3
    4    d         4   4        4   4
    5    e         5   5        5   5
    

    If you save transformed data.frame and check for mode and class, you'll get:

    > D <- transform(d, fake_char = as.numeric(fake_char), 
                        char_fac = as.numeric(char_fac))
    
    > sapply(D, mode)
           char   fake_char         fac    char_fac         num 
    "character"   "numeric"   "numeric"   "numeric"   "numeric" 
    > sapply(D, class)
           char   fake_char         fac    char_fac         num 
    "character"   "numeric"    "factor"   "numeric"   "integer"
    

    So, the conclusion is: Yes, you can convert character vector into a numeric one, but only if it's elements are "convertible" to numeric. If there's just one character element in vector, you'll get error when trying to convert that vector to numerical one.

    And just to prove my point:

    > err <- c(1, "b", 3, 4, "e")
    > mode(err)
    [1] "character"
    > class(err)
    [1] "character"
    > char <- as.numeric(err)
    Warning message:
    NAs introduced by coercion 
    > char
    [1]  1 NA  3  4 NA
    

    And now, just for fun (or practice), try to guess the output of these commands:

    > fac <- as.factor(err)
    > fac
    ???
    > num <- as.numeric(fac)
    > num
    ???
    

    Kind regards to Patrick Burns! =)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(14条)

报告相同问题?

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败