我不确定对表进行硬编码的方式是否是最好的方式。但是无论如何,我正在尝试将“总积分”列除以获取每分钟积分所需的时间,然后为该计算创建一个新列,但我似乎无法使其工作。
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)中: ‘/’对因素没有意义”
非常感谢您的帮助!
答案 0 :(得分:3)
此处的警告是有益的,您可以通过检查str()
中的table
来看到。它告诉您所有变量都是类型因子。 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创建
答案 1 :(得分:0)
@Chase已经解释了为什么以前无法创建新列,但是如果您想要一个涉及更少代码行的解决方案,则可以使用public class LambdaHandler implements RequestHandler<S3Event, String> {
// Code to fetch S3 object name here which is needed in application processing logic further
}
包(我正在使用dplyr
版本0.7.6)。
如@Ozgur Yigit所述,您无需将数据作为列表传递。实际上,您可以仅将每一列作为dplyr
的参数来传递。
编辑
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)
的更多信息:https://r4ds.had.co.nz/transform.html#add-new-variables-with-mutate
如果我的回答无济于事或不在SO响应范围之内,则表示歉意。我仍在学习如何在SO上写答案。
答案 2 :(得分:0)
您可以使用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