在一个分类变量中重新组合级别

时间:2018-01-31 19:52:42

标签: r categories

我试图通过组合分类变量的级别来简化数据分析。

此变量有6个级别 假设这个变量的名称是“蜡烛”,等级是: “永远”,“几乎总是”,“有时”,“很少”,“从不”,“从不使用”,NA

我想将“总是”和“几乎总是”重新组合为“是”,将“有时”留下来,将“很少”和“从不”留给“否”

我用过:

data <- data %>%
mutate(candle_new = ifelse(candle == "Always", "Yes", ifelse(candle == "Nearly always", "Yes", ifelse(candle == "Sometimes", "St", 
ifelse(candle == "Never", "No", ifelse(candle == "seldom", "No", NA))))))

虽然它运行并且没有显示任何错误消息,但当我检查原始数据时,它似乎不起作用。

你能帮我弄清楚我做错了吗?

谢谢!

4 个答案:

答案 0 :(得分:0)

没有足够的信息但是......难道你们内部“很少”嵌套ifelse中有一个小写的“s”吗?

答案 1 :(得分:0)

我可以看到它有效。查看数据和结果。

data <- data.frame(id = 1:7, candle = c("Always", "Nearly always", "Sometimes", "Seldom", "Never", "Never used", NA))

library(dplyr)
data <- data %>%
  mutate(candle_new = ifelse(candle == "Always","Yes",
                             ifelse(candle == "Nearly always", "Yes",
                                    ifelse(candle == "Sometimes", "St",
                                           ifelse(candle == "Never", "No", ifelse(candle == "Seldom", "No", NA))))))

data
#  id        candle candle_new
#1  1        Always        Yes
#2  2 Nearly always        Yes
#3  3     Sometimes         St
#4  4        Seldom         No
#5  5         Never         No
#6  6    Never used       <NA>
#7  7          <NA>       <NA>

答案 2 :(得分:0)

我认为在这种情况下使用ifelsematch代替left_join,而不是match_df

首先,我们创建一个名为 old new 1 Always Yes 2 Nearly Always Yes 3 Sometimes Sometimes 4 Seldom No 5 Never No 的data.frame,如下所示:

left_join

然后我们从该data.frame中查找新值。我们可以使用set.seed(2) library(dplyr) # the match dataframe match_df = data.frame(old = c('Always','Nearly Always','Sometimes','Seldom','Never'), new = c('Yes','Yes','Sometimes','No','No')) # sample data df = data.frame(candle = sample(match_df$old,12,TRUE)) # option 1, with match df %>% mutate(candle_new = match_df$new[match(candle,match_df$old)]) # option 2, left_join df %>% left_join(match_df,by=c('candle'='old')) %>% rename(candle_new=new) 或匹配:

来实现
$(document).ready(function(){
    $("#date1").datepicker();
    $("#date1").datepicker('setDate', 'today');
});

<input type="text" id="date1">

希望这有帮助!

答案 3 :(得分:0)

car包有一个优雅的(IMO)recode函数,可以处理多个值。

yes.set <- c('Always','Nearly always')
no.set <- c('Seldom','Never','Never used')
# made up data
data <- data.frame(vals=sample(candles,50,replace=T))

data$vals<-recode(data$vals,"yes.set='Yes'; no.set='No'")

任何超出所需设置的内容都可以使用NA参数设置为else。您必须包含&#34;有时&#34;值明确,首先。

data$vals<-recode(data$vals,"yes.set='Yes'; no.set='No';'Sometimes'='Sometimes';else=NA")