1
我想重塑一些數據從長格式到寬格式,但讓我困惑的是如何根據原始長格式中的特定列重命名新列。如何在將R從長格式轉換爲寬格式時創建新列名?
Name <- c("Brian","Brian","Brian")
Age <- c(22,22,22)
Date <- c("2017.1.3","2017.1.3","2017.1.4")
School <- c("PH","En","Math")
Score <- c(100,99,98)
Course <- c("Epi751","Python","Statistics")
data <- data.frame(Name, Age, Date, School, Score, Course)
data
而且表看起來像
Name Age Date School Score Course
1 Brian 22 2017.1.3 PH 100 Epi751
2 Brian 22 2017.1.3 En 99 Python
3 Brian 22 2017.1.4 Math 98 Statistics
所以,我怎麼能變成這樣?
Name Age Date_Epi751 School_Epi751 Score_Epi751 Date_Python School_Python Score_Python Date_Statistics School_Statistics Score_Statistics
Brian 22 2017.1.3 PH 100 2017.1.3 En 99 2017.1.4 Math 98