简体   繁体   English

在R中将一列拆分为多列

[英]Splitting up a column into multiple columns in r

I am trying to tidy some data on the number of students enrolled in different subjects at different year levels separated by gender over 14 years. 我试图整理一些数据,以不同年龄段的不同科目的入学人数(按性别划分)超过14年。

I'm not sure what dplyr function to use because I don't want to make the observations variable names I want to group them and put them in variables. 我不确定要使用什么dplyr函数,因为我不想为观察变量命名,而是想将它们分组并放入变量中。

Row Labels    Year   students 
Biology       2004   26252
Female        2004   16091 
Year 9        2004   40
Year 10       2004   1857
Year 11       2004   8807
Year 12       2004   5386
Year 13       2004   1
Male          2004   10161
Year 9        2004   382
Year 10       2004   1301
Year 11       2004   5062
Year 12       2004   3118
Year 13       2004   298
Biotechnology 2004   2682
Female        2004   1257

I want it to look something like this with gender in one column, subject in another and year level in another. 我希望它看起来像这样,在一列中显示性别,在另一列中显示主题,在另一列中显示年份。

Subject          Gender    Year  Year level  students
Biology          Female    2004  Year 9       16091
Biotechnology    Female    2004  Year 10      1257

df <- structure(list(`Row Labels` = c("Biology", "Female", "Year 9", 
"Year 10", "Year 11", "Year 12", "Year 13", "Male", "Year 9", 
"Year 10", "Year 11", "Year 12", "Year 13", "Biotechnology", 
"Female"), Year = c(2004, 2004, 2004, 2004, 2004, 2004, 2004, 
2004, 2004, 2004, 2004, 2004, 2004, 2004, 2004), students = c(26252, 
16091, 40, 1857, 8807, 5386, 1, 10161, 382, 1301, 5062, 3118, 
298, 2682, 1257)), row.names = c(NA, -15L), class = c("tbl_df", 
"tbl", "data.frame"))

This will do the trick but you have to manually specify all the subjects in your dataset: 这可以解决问题,但是您必须手动指定数据集中的所有subjects

library(tidyr)
library(dplyr)

subjects <- c("Biology", "Biotechnology")

df %>% 
  mutate(
    Subject = if_else(`Row Labels` %in% subjects, `Row Labels`, NA_character_),
    Gender = if_else(`Row Labels` %in% c("Male", "Female"), `Row Labels`, NA_character_)
  ) %>% 
  fill(Subject, Gender) %>% 
  filter(`Row Labels` != Subject, `Row Labels` != Gender) %>% 
  select(Subject, Gender, Year, `Year level` = `Row Labels`, students)

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

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