我刚刚开始在Rstudio工作,但我仍在努力。我得到了一个名为Concerts的数据集,其中有一个名为“ instrument1”的列,其中命名了各种不同的乐器。我被问到以下问题:
在音乐会表中,将(第一个)乐器更改为一个因子,并将所有类型的萨克斯管加入“萨克斯管”类别。提示:检查该因子的频率分布,以找到所有类型的萨克斯管。
concerts$instrument1 <- as.factor(concerts$instrument1)
concerts %>%
count(instrument1) # to find out what kind of saxes there are. This turned out with the following outcome:
instrument1 n
bariton sax 21
alto sax 103
bass sax 5
tenor sax 377
soprano sax 22
#there also used to be a category with the name "saxophones" but that one has disappeared since running the code below.
#this code does not work, in the summary all instruments are still separate.
concerts <- concerts %>%
mutate(instrument1 = forcats::fct_recode(instrument1,
"alto sax" = "saxophones",
"bariton sax" = "saxophones",
"bass sax" = "saxophones",
"soprano sax" = "saxophones",
"tenor sax" = "saxophones",
))
summary(concerts$instrument1)
#this did not work either.
concerts %>% fct_collapse(saxophones = c("alto sax","bariton sax", "bass sax", "soprano sax", "tenor sax"))
summary(concerts$instrument1)
mutate(instrumentnew = fct_collapse(instrument1,
saxophones = c("alto sax", "bariton sax", "bass sax", "saxophones", "soprano sax", "tenor sax")
)) %>%
有人可以帮助我吗?