替换变量中的值

时间:2017-07-22 18:44:01

标签: r dataframe replace

我在R schools中有数据有94个变量。其中我在一组中选择了3个变量进行分析:

schools_set <- data.frame(schools$Schoolname, schools$SchoolGenderID, and School$)

SchoolGenderID按列分列(1s和2s)1为男性,2为女性。我的问题是,如何在同一数据框内的变量"Male"中分别用"Female"schools$SchoolGenderID替换这些1和2?

3 个答案:

答案 0 :(得分:0)

schools$SchoolGenderID[schools$SchoolGenderID == 1] <- "Male"
schools$SchoolGenderID[schools$SchoolGenderID == 2] <- "Female"

或者

schools$SchoolGenderID <- ifelse(schools$SchoolGenderID == 1, "Male", "Female")

在这种特殊情况下推荐后者。

答案 1 :(得分:0)

尝试data.table方式:)

schools = data.table (schools)

schools [ SchoolGenderID == "Male", "SchoolGenderID" := "1"]
schools [ SchoolGenderID == "Female", "SchoolGenderID" := "2"]
schools = schools [order (Schoolname, SchoolGenderID)]

结果性别列不是数字,而是字符类型,因为初始列是字符类型。如果需要数字列,请创建新列:

schools [ SchoolGenderID == "Male", "SchoolGenderNo" := 1]
schools [ SchoolGenderID == "Female", "SchoolGenderNo" := 2]

答案 2 :(得分:0)

这是dplyr

中的一种方式
library(tidyverse)


schools_set <- schools %>%
                 select(Schoolname, SchoolGenderID) %>% # Make your subset 
                 mutate(
                   school_gender_id = ifelse(SchoolGenderID == 1, 
                                              "Male", "Female")
                 ) %>% 
                 mutate(
                   school_gender_id = as.factor(school_gender_id)
                 ) %>%
                 arrange(school_gender_id) # Order dataframe

mutate允许您修改变量并创建新变量。安排做秩序的工作。如果订单不正确,您可以执行以下操作:arrange(desc(school_gender_id))%>%被称为“管道”,意思是“在执行此操作后,转到下一个命令。”

我推荐这个教程:http://r4ds.had.co.nz/introduction.html 它是由R的创建者Hadley Wickham编写的。非常完整和有用的R编程简介。如果你学习tidyverse

,那将会更容易,也不那么特别。