【问题标题】:Converting vector to numeric and character in one go一次将向量转换为数字和字符
【发布时间】:2017-12-15 06:01:06
【问题描述】:

我是 R 的初学者,想知道是否有任何方法可以将多个向量/变量转换为所需的“类”(例如,数据集中的 3 个变量是因子,我想将这 3 个变量转换为数值变量一口气)。

下面是包含列"Product"chr 和其余列为factors 的数据集,但是我想保留"Product""Month" 作为字符和"Sales""Profit" 作为数字。

str(Conditional_function_IVY)

'data.frame':   100 obs. of  4 variables:
 $ Product: chr  "Bellen" "Bellen" "Sunshine" "Sunset" ...
 $ Month  : Factor w/ 12 levels "April","August",..: 5 5 5 5 5 5 5 5 4 4 ...
 $ Sales  : Factor w/ 88 levels " ? 501.00 "," ? 504.00 ",..: 8 13 64 16 55 78 81 29 2 52 ...
 $ Profit : Factor w/ 65 levels " ? 100.00 "," ? 101.00 ",..: 44 34 5 15 39 16 37 38 65 56 ...

我已经按照以下方式完成了它,但是它消耗了很多时间,因此我想知道是否有任何方法可以让我一次性完成。

Conditional_function_IVY$Month=as.character(Conditional_function_IVY$Month)
> Conditional_function_IVY$Sales=as.numeric(Conditional_function_IVY$Sales)
> Conditional_function_IVY$Profit=as.numeric(Conditional_function_IVY$Profit)
> str(Conditional_function_IVY)
'data.frame':   100 obs. of  4 variables:
 $ Product: chr  "Bellen" "Bellen" "Sunshine" "Sunset" ...
 $ Month  : chr  "January" "January" "January" "January" ...
 $ Sales  : num  8 13 64 16 55 78 81 29 2 52 ...
 $ Profit : num  44 34 5 15 39 16 37 38 65 56 ...

【问题讨论】:

    标签: r


    【解决方案1】:

    解决此问题的最佳方法是在创建/导入日期框架时,来自 tidyverse 的更现代的方法,例如 readrtibble 可以很好地处理猜测列类型并且不要'不自动转换为因子。

    如果这不是您的选择,那么您可以很简单地使用dplyr::mutate 进行转换。

    library(magrittr)
    library(dplyr)
    
    Conditional_function_IVY %<>%
      mutate(
        Month = as.character(Month),
        Sales = as.numeric(as.character(Sales)),
        Profit = as.numeric(as.character(Profit))
      )
    

    但是,我注意到在存储数值的结构中可以看到一些非常奇怪的值。可以使用 gsub 将它们剥离回数字。

    例如 as.numeric(gsub("[^0-9.]", "", " ? 501.00 ")) # [1] 501

    有两行数据

    使用我可以从您的问题中得出的两行您自己的数据。

    Conditional_function_IVY <- data.frame(
      Product = rep("Bellen", 2),
      Month = c("April", "August"),
      Sales = c(" ? 501.00 ", " ? 504.00 "),
      Profit = c(" ? 100.00 ", " ? 101.00 ")
    )
    
    Conditional_function_IVY %>%
      mutate(
        Month = as.character(Month),
        Sales = as.numeric(gsub("[^0-9.]", "", as.character(Sales))),
        Profit = as.numeric(gsub("[^0-9.]", "", as.character(Profit)))
      )
    
    #   Product  Month Sales Profit
    # 1  Bellen  April   501    100
    # 2  Bellen August   504    101 
    

    【讨论】:

    • 优秀的凯文 - 非常感谢
    【解决方案2】:

    我喜欢 Kevin 的方法,但我不喜欢 as.numeric(gsub("[^0-9.]", "", as.character(...)) 的复制/粘贴/编辑。如果你甚至有 10 列,这将是乏味的,如果你有 100 列,那将是完全不切实际的。我会定义一个小实用函数并执行以下操作:

    # define helper function
    sub_convert = function(x) as.numeric(gsub("[^0-9.]", "", as.character(...))
    
    # using base R
    to_convert = names(Conditional_function_IVY)[sapply(Conditional_function_IVY, is.factor)]
    Conditional_function_IVY[to_convert] = lapply(
        Conditional_function_IVY[to_convert],
        sub_convert
    )
    
    # or using dplyr
    library(dplyr)
    Conditional_function_IVY = mutate_if(
        Conditional_function_IVY,
        is.factor,
        sub_convert
    )
    

    这可以更好地扩展,并且还具有以下优点:如果您需要调整 sub_convert 函数,您只需在一个地方编辑它,而不是每次使用它。

    【讨论】:

    • 我喜欢你的功能来概括文本清理,并同意我的方法可能对很多列很乏味。但是,我也倾向于遵循 Hadley 的经验法则,在多次 (3) 次重复后编写函数。
    猜你喜欢
    • 2023-04-01
    • 2014-04-10
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    • 2020-02-27
    • 1970-01-01
    • 2012-05-27
    相关资源
    最近更新 更多