我有一个带有两列的以下格式的数据帧。
第一列是名称,第二列是描述层次结构中的级别的描述。 即学校A和学校B处于同一水平,班级比学校低一级,所以他们的学习时间更长,并且在学校des1 @ 123或1 @ 124之后还有一个学习。然后学生...... ..
name des
School A 1@123
School B 1@124
Class A 1@123@230
Class B 1@123@231
Class C 1@124@232
Class D 1@124@233
Student 1 1@123@230@001
Student 2 1@123@231@002
Student 3 1@123@231@003
Student 4 1@124@232@004
Student 5 1@124@233@005
根据他们的说法,我想把数据重新排列成几列,比如旋转。即不确定我们在R中是否有任何相关代码?感谢。
School A Class A Student 1
School A Class B Student 2
School A Class B Student 3
School B Class C Student 4
School B Class D Student 5
答案 0 :(得分:1)
这应该可以帮到你,其余的只是重新编码或查找
library(tidyr)
library(dplyr)
separate(df, des, into = c("School_1", "School_2", "Class"),
sep = "@", extra = "drop") %>%
unite(School, School_1:School_2) %>%
.[grepl("Student", .$name), ]
# name School Class
# 7 Student 1 1_123 230
# 8 Student 2 1_123 231
# 9 Student 3 1_123 231
# 10 Student 4 1_124 232
# 11 Student 5 1_124 233
# 12 Student 5 1_124 233