简体   繁体   中英

logical to factors (or strings) in data.frame

How can recode logical to factors (or strings) in data.frame?

data <- data.frame(year = c(2015, 2015, 2016, 2016),
               column2 = c(4, NA, 9, 1))


library (dplyr)

missing_data <- data %>%
              count(year, complete.cases(column2)) 

names(missing_data)[2] = "col2" 

My results:

year  col2     n
(dbl) (lgl) (int)
2015 FALSE     1
2015  TRUE     1
2016  TRUE     2 

What I want:

year  col2      n
(dbl)        (int)
2015  unknown    1
2015  known      1
2016  known      2 

What I tried (in dplyr chain):

mutate(col2 = as.factor(col2))

这个人应该这样做。

missing_data$col2 <- factor(missing_data$col2, labels=c("unknown", "known"))

Besides the obvious method as shown by richard lindgren , you could also do this inside the dplyr chain. You can create an numeric index from the logicals [col2 + 1] and use that to assign the levels c('unknown','known') :

dat %>%
  count(year, col2 = complete.cases(column2)) %>%
  mutate(col2 = c('unknown','known')[col2 + 1])

or with ifelse :

dat %>%
  count(year, col2 = complete.cases(column2)) %>%
  mutate(col2 = ifelse(col2,'known','unknown'))

which both give:

   year    col2     n
  (dbl)   (chr) (int)
1  2015 unknown     1
2  2015   known     1
3  2016   known     2

If you want a factor as result, you can wrap it in factor : factor(c('unknown','known')[col2 + 1]) or factor(ifelse(col2,'known','unknown')) .


If you want to incorporate the method as shown by richard lindgren in the dplyr -chain, you will have to ungroup first:

dat %>%
  count(year, col2 = complete.cases(column2)) %>%
  ungroup() %>% 
  mutate(col2 = factor(col2, labels = c('unknown','known')))

which will give you the desired result as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM