将因子类别从一列合并到一个新列

时间:2017-07-13 18:40:11

标签: r

这是我第一次发帖提问。我对R很新。我在很长一段时间内都在寻找答案而没有找到答案。所以这里。我有一个非常大的数据集(超过140K obs),一列包含" programtype"的类别,选项是:

  • 联邦机构
  • 联邦机构/大学
  • 国家调查计划
  • LTER
  • 大学
  • 非营利机构
  • 国家机构
  • 州机构/公民监测计划
  • 州机构/大学/公民监测计划
  • Tribal Agency

我想要做的是创建一个新列,我将一些类别合并在一起。我想:

  • [联邦机构,联邦机构/大学,国家调查计划] 转换为 联邦机构/大学
  • [LTER,大学] 转换为 LTER / University
  • [非营利机构] 转换为 非营利机构
  • [国家机构] 转换为 州代理机构
  • [国家机构/公民监测计划,州机构/大学/公民监测计划] 转换为 公民科学监测计划
  • [部落代理] 转换为 部落代理

其中一些将保持不变。我已经尝试了ifelse语句,但似乎很难确定原始列中的内容并返回NAs以进行大量观察。我检查了所有的拼写,所以不是这样。这是我在这里根据另一个答案尝试的。我的数据集的名称是TP_state,其他列的名称是lagoslakeid。但是,它无法正常工作。任何帮助将不胜感激!

x <- c(TP_state$programtype) 
y <- c(TP_state$lagoslakeid)
df <- data.frame(x,y)
DT <- data.table(df)
DT[, Program_Type := ifelse(x %in% c("Federal Agency", "Federal Agency/University", "National Survey Program"), "Federal Agency/University",
                 ifelse(x %in% c("LTER", "University"), "LTER/University",
                 ifelse(x %in% c("Non-Profit Agency"), "Non-Profit Agency",       
                 ifelse(x %in% c("State Agency"), "State Agency",
                 ifelse(x %in% c("State Agency/University/Citizen Monitoring Program", "State Agency/Citizen Monitoring Program"), "Citizen Monitoring Program", 
                 ifelse(x %in% c("Tribal Agency"), "Tribal Agency", NA))))))]  

2 个答案:

答案 0 :(得分:0)

我会尝试这样的事情。请告诉我它是否适合你!

for(i in 1:length(df$column_with_factors)){
  if(grepl(pattern = 'federal agency|national survey program', x = df$column[i], ignore.case = TRUE)){
    x <- 'Federal Agency/University'
  } else if(grepl(pattern = '^lter$|^university$', x = df$column[i], ignore.case = TRUE)){
    x <- 'LTER/University'
  } else if(grepl(pattern = 'non-profit agency', x = df$column[i], ignore.case = TRUE)){
    x <- 'Non-profit Agency'
  } else if(grepl(pattern = '^state agency$', x = df$column[i], ignore.case = TRUE)){
    x <- 'State Agency'
  } else if(grepl(pattern = 'state agency/(citizen monitoring program|university/citizen monitoring program)', x = df$column[i], ignore.case = TRUE)){
    x <- 'Citizen Science Monitoring Program'
  } else if(grepl(pattern = 'tribal agency', x = df$column[i], ignore.case = TRUE)){
    x <- 'Tribal Agency'
  } else x <- NA
}

df$column_with_factors <- as.factor(df$column_with_factors)

但这会更快:

df$column_with_factors <- sapply(df$column_with_factors, function(x){
  if(grepl(pattern = 'federal agency|national survey program', x = x, ignore.case = TRUE)){
    x <- 'Federal Agency/University'
  } else if(grepl(pattern = '^lter$|^university$', x = x, ignore.case = TRUE)){
    x <- 'LTER/University'
  } else if(grepl(pattern = 'non-profit agency', x = x, ignore.case = TRUE)){
    x <- 'Non-profit Agency'
  } else if(grepl(pattern = '^state agency$', x = x, ignore.case = TRUE)){
    x <- 'State Agency'
  } else if(grepl(pattern = 'state agency/(citizen monitoring program|university/citizen monitoring program)', x = x, ignore.case = TRUE)){
    x <- 'Citizen Science Monitoring Program'
  } else if(grepl(pattern = 'tribal agency', x = x, ignore.case = TRUE)){
    x <- 'Tribal Agency'
  } else x <- NA
})

df$column_with_factors <- as.factor(df$column_with_factors)

答案 1 :(得分:0)

forcats包非常适合重新编码这样的任务。

首先创建一些演示数据......

library(tidyverse)
library(forcats)

df <-
  tibble(
    programtype = c(
      "Federal Agency",
      "Federal Agency",
      "Federal Agency",
      "State Agency/University/Citizen Monitoring",
      "State Agency/University/Citizen Monitoring Program",
      "Federal Agency/University",
      "National Survey Program",
      "LTER",
      "University",
      "Non-Profit Agency",
      "Non-Profit Agency",
      "Non-Profit Agency",
      "Non-Profit Agency",
      "Non-Profit Agency",
      "State Agency",
      "State Agency",
      "State Agency/Citizen Monitoring Program",
      "State Agency/University/Citizen Monitoring Program",
      "Tribal Agency",
      "Tribal Agency",
      "Tribal Agency"
    ),
    ID = 1:21
  )

然后使用fct_recode替换值...

df %>%
  mutate(
    new_categories = fct_recode(
      programtype,
      "Federal Agency/University" = "Federal Agency",
      "Federal Agency/University" = "Federal Agency/University",
      "Federal Agency/University" = "National Survey Program",
      "LTER/University" = "LTER",
      "LTER/University" = "University",
      "Citizen Science Monitoring Program" = "State Agency/Citizen Monitoring Program",
      "Citizen Science Monitoring Program" = "State Agency/University/Citizen Monitoring"
    )
  )