有条件地替换 R 中的分类值

Conditionally replace categorical values in R

我是 R 的新手,并且一直在这个论坛上咨询我以前的一些 R 问题。但是,我似乎找不到我当前的答案。

我有一个包含多列的大数据集。我想根据另一列中的值替换一列中的某些值。这是一个示例:

organization                                    organization_type                
[1,] "Human Relief Foundation"                  "NGO"                            
[2,] "Management Systems International"         "Other"                          
[3,] "World Vision"                             "NGO"                            
[4,] "European Disaster Volunteers"             "NGO"                            
[5,] "Management Systems International"         "Other"                          
[6,] "International Committee of the Red Cross" "Red Cross/Red Crescent Movement"
[7,] "International Committee of the Red Cross" "Red Cross/Red Crescent Movement"
[8,] "Development Alternatives"                 "Consultancy"                    

上面的数据集显示 "Other" 在 organization_type 下的值 "Management Systems International"。我想用 "Consultancy" 替换 "Other"。我该怎么做?

我已经按照另一个论坛中的建议尝试了以下方法,但它只保留了过滤后的数据:

library(dplyr)

data_df <- data_df %>% filter(organization == "Management Systems International" 
           & organization_type == "Other") %>%  
           mutate(organization_type = "Consultancy")

有没有办法 "unfilter" R 中的数据也包含原始数据条目和过滤后的数据? Excel 这样做但是很难在 Excel 中处理大数据集。

谢谢!

您可以使用包 stringrifelse

您的数据(它的子集,用于说明目的)。

a <- c("Human Relief Foundation", "Management Systems International", "World Vision", "World Vision")   
b <- c("NGO", "Other", "NGO", "Other")
df <- as.data.frame(cbind(a,b))
df
#                                 a     b
#1          Human Relief Foundation   NGO
#2 Management Systems International Other
#3                     World Vision   NGO
#4                     World Vision Other

然后替换数据的特定部分。

library(stringr)
df$b <- ifelse(df$a=="Management Systems International", 
   str_replace(as.character(df$b), "Other", "Consultancy"), as.character(df$b))
df
#                                 a           b
#1          Human Relief Foundation         NGO
#2 Management Systems International Consultancy
#3                     World Vision         NGO
#4                     World Vision       Other

使用 dplyr,

data_df %>% mutate(organization_type = ifelse(
      organization == "Management Systems International",
      "Consultancy",
      organization_type))

                              organization               organization_type
1                  Human Relief Foundation                             NGO
2         Management Systems International                     Consultancy
3                             World Vision                             NGO
4             European Disaster Volunteers                             NGO
5         Management Systems International                     Consultancy
6 International Committee of the Red Cross Red Cross/Red Crescent Movement
7 International Committee of the Red Cross Red Cross/Red Crescent Movement
8                 Development Alternatives                     Consultancy

使用 data.table 包,您可以按如下方式执行此操作:

# Install if necessary
if (!require("data.table")) install.packages("data.table")
# Load the data.table package
library(data.table)

# Convert data_df to a data.table 
data_dt <- data.table(data_df) %>%
  # Where organization_type equals 'Other', replace organization_type to 'Consultancy'
  .[organization_type == "Other", organization_type := "Consultancy"]

# Print result
print(data_dt)

结果:

                               organization               organization_type
1:                  Human Relief Foundation                             NGO
2:         Management Systems International                     Consultancy
3:                             World Vision                             NGO
4:             European Disaster Volunteers                             NGO
5:         Management Systems International                     Consultancy
6: International Committee of the Red Cross Red Cross/Red Crescent Movement
7: International Committee of the Red Cross Red Cross/Red Crescent Movement
8:                 Development Alternatives                     Consultancy