这是我第一次发帖提问。我对R很新。我在很长一段时间内都在寻找答案而没有找到答案。所以这里。我有一个非常大的数据集(超过140K obs),一列包含" programtype"的类别,选项是:
我想要做的是创建一个新列,我将一些类别合并在一起。我想:
其中一些将保持不变。我已经尝试了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))))))]
答案 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"
)
)