2016-07-19 2 views

답변

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

길이가 긴 테이블이있어서 와이드 포맷을 원합니다. 이를 위해 dcast() 기능을 사용하십시오 (패키지는 reshape2).

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")