从ANOVA的宽幅到长格式重塑

时间:2013-11-20 15:08:10

标签: r command reshape

在我的数据集上使用na.omit(pruefung)后,我现在希望将其变为长格式。任何人都可以帮助我,因为我是R的新手?我必须使用哪个命令?

下面是数据集:http://www.file-upload.net/download-8313566/pruefung.csv.html

1 个答案:

答案 0 :(得分:1)

这些是数据集中的names

names(x)
#  [1] "nr"     "sex"    "kind"   "alter"  "gs.1"   "ru.1"   "gs.2"  
#  [8] "ru.2"   "gs.3"   "ru.3"   "gruppe

人们会假设需要进入“长”形式的列是以gs.ru.开头的列。查看数据,可以假设“id”变量是“nr”列。

记住这些假设,我会以下列方式使用reshape(假设我的data.frame被称为“x”):

xL <- reshape(x, direction = "long", idvar = "nr",
              varying = grep("^gs|^ru", names(x)), sep = ".")
head(xL)
#     nr      sex kind alter   gruppe time   gs   ru
# 1.1  1 weiblich nein    27 Gruppe 1    1 3.25 1.25
# 2.1  2 m?nnlich   ja    28 Gruppe 1    1 4.50 3.50
# 3.1  3 weiblich nein    27 Gruppe 1    1 5.00 4.50
# 4.1  4 weiblich nein    26 Gruppe 1    1 4.75 3.25
# 5.1  5 weiblich nein    32 Gruppe 1    1 4.25 3.25
# 6.1  6 weiblich nein    29 Gruppe 1    1 1.00 1.00
相关问题