[英]Change data from wide to long format in r
我有數據,其中兩個評分者對學生在多個問題上的評分。 每行包含以下變量:
....然后重復多個項目。
看起來像這樣:
Student_ID <- c(1:4)
Item1_first_rater_id <- c(1,2,1,2)
Item1_first_rating <- c(2,3,4,2)
Item1_second_rater_id <- c(2,3,2,3)
Item1_second_rating <- c(4,5,3,2)
Item2_first_rater_id <- c(4,2,5,1)
Item2_first_rating <- c(2,3,4,2)
Item2_second_rater_id <- c(6,7,2,3)
Item2_second_rating <- c(3,4,5,4)
wide <- data.frame(Student_ID, Item1_first_rater_id, Item1_first_rating,
Item1_second_rater_id, Item1_second_rating,
Item2_first_rater_id, Item2_first_rating,
Item2_second_rater_id, Item2_second_rating)
我需要這樣的長格式數據:
Student_ID <- c(1:4)
Item_number <- c(1,1,2,2)
Rater_id <- c(1:4)
Score <- c(2,3,4,5)
long <- data.frame(Student_ID, Item_number, Rater_id, Score)
關於如何重塑的任何想法?
謝謝。
尚不清楚您要做什么(換句話說,您要如何精確地轉換源數據)。 這是一個至少可能使您更接近所需輸出的猜測。
您的“寬”數據集中的names
似乎包含三組信息:(1)項目編號,(2)“時間”(第一或第二),以及(3)另一個變量(“評級”或“評級者ID”)。
我們可以使用melt
, colsplit
和dcast
來方便我們的重塑。
melt
集 library(reshape2)
orignames <- names(wide) # Store the original names so we can replace them
names(wide) <- gsub("Item([0-9])_(.*)_(rater_id|rating)",
"\\1\\.\\2\\.\\3", names(wide))
# "melt" the dataset
m.wide <- melt(wide, id.vars="Student_ID")
head(m.wide)
# Student_ID variable value
# 1 1 1.first.rater_id 1
# 2 2 1.first.rater_id 2
# 3 3 1.first.rater_id 1
# 4 4 1.first.rater_id 2
# 5 1 1.first.rating 2
# 6 2 1.first.rating 3
colsplit
創建新列 m.wide <- cbind(m.wide,
colsplit(m.wide$variable, "\\.",
c("Item", "Time", "Var")))
head(m.wide)
# Student_ID variable value Item Time Var
# 1 1 1.first.rater_id 1 1 first rater_id
# 2 2 1.first.rater_id 2 1 first rater_id
# 3 3 1.first.rater_id 1 1 first rater_id
# 4 4 1.first.rater_id 2 1 first rater_id
# 5 1 1.first.rating 2 1 first rating
# 6 2 1.first.rating 3 1 first rating
dcast
重塑數據 dcast(m.wide, Student_ID + Item ~ Time + Var, value.var="value")
# Student_ID Item first_rater_id first_rating second_rater_id second_rating
# 1 1 1 1 2 2 4
# 2 1 2 4 2 6 3
# 3 2 1 2 3 3 5
# 4 2 2 2 3 7 4
# 5 3 1 1 4 2 3
# 6 3 2 5 4 2 5
# 7 4 1 2 2 3 2
# 8 4 2 1 2 3 4
開關什么是左,什么是對的右~
你的數據會影響“形”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.