简体   繁体   English

如何重命名多个观察值?

[英]How to rename multiple observations?

I have a tibble with three columns in r.我在 r 中有一个包含三列的小标题。 One of the columns are "week_days".其中一列是“week_days”。 All the observations in this column are abbreviated like this: (mo,tu,we,th,fr,sa,su).此列中的所有观察值都缩写为:(mo,tu,we,th,fr,sa,su)。 Though I want to change them to (monday, tuesday... and so on).虽然我想将它们更改为(星期一,星期二......等等)。

any idea on how I can do this?关于我如何做到这一点的任何想法?

We can create a named vector of key/value to match and replace the values in the original data column in base R我们可以创建一个键/值的命名vector来匹配和替换base R中原始数据列中的值

nm1 <- setNames(c("monday", 'tuesday', 'wednesday', 'thursday', 
       'friday', 'saturday', 'sunday'),
      c('mo', 'tu', 'we', 'th', 'fr', 'sa', 'sun')   )
df1$week_days <- nm1[df1$week_days]

Other solutions:其他解决方案:

with plyrplyr

df1$week_days <- plyr::mapvalues(
  df1$week_days,
  c('mo', 'tu', 'we', 'th', 'fr', 'sa', 'sun'),
  c("monday", 'tuesday', 'wednesday', 'thursday','friday', 'saturday', 'sunday')
)
      

with dplyr recode :用 dplyr recode

df1 %>%
  mutate(week_days = recode(week_days,
                            mo = "monday",
                            tu = 'tuesday'
                            we = 'wednesday',
                            th = 'thursday',
                            fr = 'friday',
                            sa = 'saturday', 
                            sun = 'sunday'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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