r - Get columns from column's values -


this question has answer here:

the dataframe contains information in format:

type        value ------------------- cata        1 catb        2 cata        3 

my target convert dataframe format (type's values columns):

cata        catb -----------------  1          -   -          2 3         - 

i have been looking "dummy variables" it's not need. ¿could give me ideas?

 library(reshape2)  df <- data.frame(type=c("cata","catb","cata"),value=c("one","two","three"))  df  #   type value  # 1 cata   1  # 2 catb   2  # 3 cata 3   dcast(df,value~type)  #   value  cata catb  # 1   1   1 <na>  # 2 3 three <na>  # 3   2  <na>  2   dcast(df,type~value)  #   type  1 3  2  # 1 cata  1 3 <na>  # 2 catb <na>  <na>  2 

to preserve order of value

 df$type <- factor(df$type,c("cata","catb"))  df$value <- factor(df$value,c("one","two","three"))   dcast(df,type~value)  #   type  1  2 3  # 1 cata  1 <na> 3  # 2 catb <na>  2  <na>   dcast(df,value~type)  #   value  cata catb  # 1   1   1 <na>  # 2   2  <na>  2  # 3 three 3 <na> 

Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -