简体   繁体   中英

Narrow format and adding columns with dplyr and tidyr

I am trying to use dplyr and tidyr to format a data frame. I would like to transform this data frame

df_ex1=data.frame(CODE=c("A","B","C"),
                  Red=c(11.4,10.9,11.4),
                  Blue=c(0.57,0.89,1.19),
                  Purple=c(0.40,3.50,"NA"))

#> df_ex1
#  CODE  Red Blue Purple
#1    A 11.4 0.57    0.4
#2    B 10.9 0.89    3.5
#3    C 11.4 1.19     NA

To this data frame:

df_ex2=data.frame(CODE=c("A","B","C"),
                  TYPE=c("One","One","Two"),
                  PARAMETAR=c("Red","Blue","Purple"),
                   VALUE=c(11.4,0.57,0.4))

#> df_ex2
#  CODE TYPE PARAMETAR VALUE
#1    A  One       Red 11.40
#2    B  One      Blue  0.57
#3    C  Two    Purple  0.40

I know I have to use the gather function, but I do not know to add the column type , or how to merge the columns properly.

Could someone show me how to do this?

library(tidyr)
df_long = gather(df_ex1, key = Parameter, value = Value, -CODE)

TYPE value should be a brand new column added to the new data frame. The idea is that PURPLE is always TWO and the other two are always ONE

Just add a column with this definition:

df_long$TYPE = ifelse(df_long$Parameter == "Purple", "TWO", "ONE")

  df_long
#   CODE Parameter Value TYPE
# 1    A       Red  11.4  ONE
# 2    B       Red  10.9  ONE
# 3    C       Red  11.4  ONE
# 4    A      Blue  0.57  ONE
# 5    B      Blue  0.89  ONE
# 6    C      Blue  1.19  ONE
# 7    A    Purple   0.4  TWO
# 8    B    Purple   3.5  TWO
# 9    C    Purple    NA  TWO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM