所以我有一个很长的序列数据集。每列(从 t1 到 t...n)都具有相同的级别或类别。共有200多个类别或级别和144列(变量)。
id t1 t2 t3 t...n
"1" "eating" "tv" "conversation" "..."
"2" "sleep" "driving" "relaxing" "..."
"3" "drawing" "kissing" "knitting" "..."
"..." "..." "..." "..." "..."
变量 t1 具有相同的级别有 t2 等等。我需要的是对每一列进行循环式重新编码(但避免循环)。
我想避免通常的
seq$t1[seq$t1== "drawing"] <- 'leisure'
seq$t1[seq$t1== "eating"] <- 'meal'
seq$t1[seq$t1== "sleep"] <- 'personal care'
seq$t1[seq$t1== "..."] <- ...
最方便的重新编码风格是这样的
c('leisure') = c('drawing', 'tv', ...)
这将帮助我更好地将变量聚类到更大的类别中。
最近出现的 R 中有一些新的更简单的重新编码方法吗?你会建议我用什么?
这是我的真实数据集的样本,10 个受访者(行)的 5 次重复观察(列)。
dtaSeq = structure(c("Wash and dress", "Eating", "Various arrangements", "Cleaning dwelling", "Ironing", "Activities related to sports",
"Eating", "Eating", "Other specified construction and repairs",
"Other specified physical care & supervision of a child", "Wash and dress",
"Filling in the time use diary", "Food preparation", "Wash and dress",
"Ironing", "Travel related to physical exercise", "Eating", "Eating",
"Other specified construction and repairs", "Other specified physical care & supervision of a child",
"Wash and dress", "Filling in the time use diary", "Food preparation",
"Wash and dress", "Food preparation", "Wash and dress", "Eating",
"Eating", "Other specified construction and repairs", "Other specified physical care & supervision of a child",
"Wash and dress", "Filling in the time use diary", "Baking",
"Teaching the child", "Food preparation", "Wash and dress", "Eating",
"Eating", "Other specified construction and repairs", "Other specified physical care & supervision of a child",
"Dish washing", "Unspecified TV watching", "Reading periodicals",
"Teaching the child", "Food preparation", "Reading periodicals",
"Eating", "Eating", "Other specified construction and repairs",
"Feeding the child", "Laundry", "Unspecified TV watching", "Cleaning dwelling",
"Teaching the child", "Eating", "Eating", "Eating", "Eating",
"Other specified construction and repairs", "Feeding the child"),
.Dim = c(10L, 6L), .Dimnames = list(c("1", "2", "3", "4",
"5", "6", "7", "8", "9", "10"), c("act1.050", "act1.051", "act1.052",
"act1.053", "act1.054", "act1.055")))