【问题标题】:How to take to two columns of data, perform a calculation and create a new column with that data如何获取两列数据,执行计算并使用该数据创建一个新列
【发布时间】:2019-02-10 20:41:41
【问题描述】:

我不确定我硬编码此表的方式是否是最好的方式。但无论如何,我正在尝试获取总分列并将其除以获得每分钟积分所需的时间,然后为该计算创建一个新列,但我似乎无法使其工作。

table <- data.frame(list(Question=c("Q1", "Q2", "Q3", "Q4" , "Q5" , "Q6" , "Q7" , "Q8", "Q9"), 
                        Total_Points=c("21","5","10","14","5","5","10","5","5"), Time_needed=c("24","7","15","12","4","3","10","5","6")))
table <- transform(table, Points_per_min = table$Total_Points / table$Time_needed)

给我一​​个错误“警告信息: 在 Ops.factor(table$Total_Points, table$Time_needed) : ‘/’ 对因子没有意义”

非常感谢您的帮助!

【问题讨论】:

    标签: r


    【解决方案1】:

    此处的警告信息丰富,您可以通过检查 tablestr() 来查看。它告诉您所有变量都是类型因子。 Check here 快速了解不同的数据类型。

    将您的数据转换为数字,然后进行数学运算:

    table <- data.frame(list(Question=c("Q1", "Q2", "Q3", "Q4" , "Q5" , "Q6" , "Q7" , "Q8", "Q9"), 
                             Total_Points=c("21","5","10","14","5","5","10","5","5"), Time_needed=c("24","7","15","12","4","3","10","5","6")))
    str(table)
    #> 'data.frame':    9 obs. of  3 variables:
    #>  $ Question    : Factor w/ 9 levels "Q1","Q2","Q3",..: 1 2 3 4 5 6 7 8 9
    #>  $ Total_Points: Factor w/ 4 levels "10","14","21",..: 3 4 1 2 4 4 1 4 4
    #>  $ Time_needed : Factor w/ 9 levels "10","12","15",..: 4 9 3 2 6 5 1 7 8
    table$Total_Points <- as.numeric(as.character(table$Total_Points))
    table$Time_needed <- as.numeric(as.character(table$Time_needed))
    table$Points_per_min <- table$Total_Points / table$Time_needed
    table
    #>   Question Total_Points Time_needed Points_per_min
    #> 1       Q1           21          24      0.8750000
    #> 2       Q2            5           7      0.7142857
    #> 3       Q3           10          15      0.6666667
    #> 4       Q4           14          12      1.1666667
    #> 5       Q5            5           4      1.2500000
    #> 6       Q6            5           3      1.6666667
    #> 7       Q7           10          10      1.0000000
    #> 8       Q8            5           5      1.0000000
    #> 9       Q9            5           6      0.8333333
    

    reprex package (v0.2.1) 于 2019-02-10 创建

    【讨论】:

      【解决方案2】:

      您可以使用tidyverse,但只需要一个mutate 语句 - 与其他回答者略有不同:

      table2 <- table %>%
           mutate(Total_Points = as.numeric(as.character(Total_Points)),
           Time_needed  = as.numeric(as.character(Time_needed)),
           Points_per_min = Total_Points / Time_needed)
      
        Question Total_Points Time_needed Points_per_min
      1       Q1           21          24      0.8750000
      2       Q2            5           7      0.7142857
      3       Q3           10          15      0.6666667
      4       Q4           14          12      1.1666667
      5       Q5            5           4      1.2500000
      6       Q6            5           3      1.6666667
      7       Q7           10          10      1.0000000
      8       Q8            5           5      1.0000000
      9       Q9            5           6      0.8333333
      

      【讨论】:

        【解决方案3】:

        @Chase 已经解释了为什么您之前无法创建新列,但是如果您想要一个涉及更少代码行的解决方案,您可以使用dplyr 包(我使用的是dplyr 版本0.7.6)。

        正如@Ozgur Yigit 提到的,您不需要将数据作为列表传递。实际上,您可以将每一列作为data.frame(...) 的参数传递。

        已编辑

        library(dplyr)
        
        table <- data.frame(Question = c("Q1", "Q2", "Q3", "Q4" , "Q5" , "Q6" , "Q7" , "Q8", "Q9"), 
                            Total_Points = c("21","5","10","14","5","5","10","5","5"), 
                            Time_needed = c("24","7","15","12","4","3","10","5","6"))
        table %>% 
          mutate_at(vars(Total_Points, Time_needed), 
                    funs(as.numeric(as.character(.)))) %>% # converts the `Total_Points` and `Time_needed` cols to character first (b/c factors coded as integers under the hood) then to numeric
          mutate(Points_per_min = Total_Points / Time_needed)
        

        有关mutate()的更多信息:https://r4ds.had.co.nz/transform.html#add-new-variables-with-mutate

        PS 如果我的回复没有帮助或超出 SO 回复的范围,我们深表歉意。我还在学习如何在 SO 上写答案。

        【讨论】:

        • 这不起作用。我刚刚意识到 - 因子被编码为数字,因此如果你在第一次重新分类为字符之前重新分类为数字,你会得到错误的结果。它需要重新分类as.character 然后as.numeric
        • 谢谢@nycrefugee!你完全正确。我编辑了我的回复
        猜你喜欢
        • 2019-02-13
        • 2019-11-29
        • 1970-01-01
        • 2021-08-03
        • 1970-01-01
        • 2018-12-20
        • 2017-08-07
        • 2021-06-28
        • 2023-03-19
        相关资源
        最近更新 更多