数据框包含以下格式的信息:
Type Value
-------------------
catA one
catB two
catA three
我的目标是将数据帧转换为此格式(将类型的值转换为列):
catA catB
-----------------
one -
- two
three -
我一直在研究“虚拟变量”,但这不是我需要的。 ¿有人能给我一些想法吗?
答案 0 :(得分:3)
library(reshape2)
df <- data.frame(Type=c("catA","catB","catA"),value=c("one","two","three"))
df
# Type value
# 1 catA one
# 2 catB two
# 3 catA three
dcast(df,value~Type)
# value catA catB
# 1 one one <NA>
# 2 three three <NA>
# 3 two <NA> two
dcast(df,Type~value)
# Type one three two
# 1 catA one three <NA>
# 2 catB <NA> <NA> two
保留value
df$Type <- factor(df$Type,c("catA","catB"))
df$value <- factor(df$value,c("one","two","three"))
dcast(df,Type~value)
# Type one two three
# 1 catA one <NA> three
# 2 catB <NA> two <NA>
dcast(df,value~Type)
# value catA catB
# 1 one one <NA>
# 2 two <NA> two
# 3 three three <NA>
答案 1 :(得分:1)
你有一个长格式表,你想要一个宽格式。使用dcast()
包中的reshape2
函数。
答案 2 :(得分:0)
由于您没有使用矩形数据集,因此将结果存储在列表中可能更有意义。这可以通过unstack
:
unstack(df, form=Value~Type)
$catA
[1] "one" "three"
$catB
[1] "two"
数据强>
df <- read.table(header=T, text="Type Value
catA one
catB two
catA three")