在R中将字符串处理为数值变量

时间:2019-03-05 00:37:13

标签: r

我一直在尝试使用if-else语句和所概述的功能将538的民意测验“评分”系统(从A +转换为F-就像在学校里一样)成一系列数字

here

我也尝试使用switch()函数,但是没有任何效果。对如何完成这项工作有任何想法吗?

4 个答案:

答案 0 :(得分:2)

使用包含映射lookupdata.frame的{​​{1}} grade进行左联接比较干净。这样一来,您就避免了很多gradenumber语句。

以下是可重现的示例:

ifelse

生成# Grades grades <- paste0(rep(LETTERS[c(1:4,6)], each = 3), c("+", "", "-"))[-c(1, 13)] grades #[1] "A" "A-" "B+" "B" "B-" "C+" "C" "C-" "D+" "D" "D-" "F" "F-" lookup

data.frame

我们现在生成一些样本数据lookup <- data.frame( grade = grades, gradenumber = length(grades):1) ,然后在PollsFiltered上用left_join生成lookup数据。

grade

答案 1 :(得分:0)

不需要ifelse,您可以执行以下操作:

df$numericGrade = NA
df$numericGrade[df$grade=="A"] = 13
df$numericGrade[df$grade=="A-"] = 12
df$numericGrade[df$grade=="B+"] = 11
df$numericGrade[df$grade=="C-"] = 6

例如:

df = data.frame(Name = c("John", "Mary", "Timmy", "Susan"), grade = c("B+", "A", "C-", "DNF"))

   Name grade
1  John    B+
2  Mary     A
3 Timmy    C-
4 Susan   DNF

应用前几行将导致:

   Name grade numericGrade
1  John    B+           11
2  Mary     A           13
3 Timmy    C-            6
4 Susan   DNF           NA

当然,每种情况只需要一行,而我的示例还不完整。

使用合并

另一种选择是使用merge。在这种情况下,您需要具有成绩转换的另一个数据框:

gradesDict = data.frame(letter = c("A", "A-", "B+", "C-"),
                        number = c(13, 12, 11, 6))

  letter number
1      A     13
2     A-     12
3     B+     11
4     C-      6

然后执行:

df = merge(df, gradesDict, by.x = "grade", by.y = "letter", all.x = T)

  grade  Name number
1     A  Mary     13
2    B+  John     11
3    C- Timmy      6
4   DNF Susan     NA

答案 2 :(得分:0)

使用dplyr的另一种方法是使用case_when语句,如下所示:

df$Num_grade <- NA
df <- mutate(df, Num_grade = case_when(
   grades == 'A' ~ 13,
   grades == 'A-' ~ 12,
   grades == 'B+' ~ 11,
   .
   .
   .
   grades == 'F-' ~ 1
))

Wich的工作方式类似于几个ifelse语句,但在我看来更容易理解

答案 3 :(得分:0)

这里是使用@EnableAutoConfiguration的方法(基于由莫里斯提供的MCVE:

    Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-03-05 02:21:54.857 ERROR 8662 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: At least one JPA metamodel must be present!
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1699) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:573) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:740) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869) ~[spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:780) ~[spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:412) ~[spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:333) ~[spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1277) ~[spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1265) ~[spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
    at com.server.Main.main(Main.java:23) ~[classes/:na]
Caused by: java.lang.IllegalArgumentException: At least one JPA metamodel must be present!
    at org.springframework.util.Assert.notEmpty(Assert.java:450) ~[spring-core-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.data.jpa.mapping.JpaMetamodelMappingContext.<init>(JpaMetamodelMappingContext.java:55) ~[spring-data-jpa-2.0.10.RELEASE.jar:2.0.10.RELEASE]
    at org.springframework.data.jpa.repository.config.JpaMetamodelMappingContextFactoryBean.createInstance(JpaMetamodelMappingContextFactoryBean.java:80) ~[spring-data-jpa-2.0.10.RELEASE.jar:2.0.10.RELEASE]
    at org.springframework.data.jpa.repository.config.JpaMetamodelMappingContextFactoryBean.createInstance(JpaMetamodelMappingContextFactoryBean.java:44) ~[spring-data-jpa-2.0.10.RELEASE.jar:2.0.10.RELEASE]
    at org.springframework.beans.factory.config.AbstractFactoryBean.afterPropertiesSet(AbstractFactoryBean.java:141) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1758) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1695) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    ... 16 common frames omitted