简体   繁体   中英

Transform data frame with column values as column names

I have a dataframe that looks like this:

               variable              Name Description value  SMTS
GTEX-N7MS-0007-SM-2D7W1 ENSG00000223972.4     DDX11L1     0 Blood
GTEX-N7MS-0007-SM-2D7W1 ENSG00000227232.4      WASH7P   158 Blood
GTEX-N7MS-0008-SM-4E3JI ENSG00000223972.4     DDX11L1     0  Skin
GTEX-N7MS-0008-SM-4E3JI ENSG00000227232.4      WASH7P   166  Skin
GTEX-N7MS-0011-R10A-SM-2HMJK ENSG00000223972.4     DDX11L1     0 Brain
GTEX-N7MS-0011-R10A-SM-2HMJK ENSG00000227232.4      WASH7P   209 Brain

I want to transform it such that the values in the Description column become the column names, and the values in the value column become the column values:

               variable   DDX11L1    WASH7P    SMTS
GTEX-N7MS-0007-SM-2D7W1         0       158   Blood
GTEX-N7MS-0008-SM-4E3JI         0       166    Skin
GTEX-N7MS-0011-R10A-SM-2HMJK    0       209   Brain

I tried using cast (eg dcast(final, value~Name) and other combinations too) but as I don't want any function (like mean, sum etc) to apply for the transformation, it returns me length of the objects. I just want the values as is. Any suggestions would be appreciated.

This seems to give the results you're looking for:

library(reshape2)
dcast(mydf, variable + SMTS ~ Description, value.var="value")
#                       variable  SMTS DDX11L1 WASH7P
# 1      GTEX-N7MS-0007-SM-2D7W1 Blood       0    158
# 2      GTEX-N7MS-0008-SM-4E3JI  Skin       0    166
# 3 GTEX-N7MS-0011-R10A-SM-2HMJK Brain       0    209

Try:

library(dplyr)
library(tidyr)

if dat is the dataset

 dat%>% select(-Name) %>% spread(Description, value)
#                          variable  SMTS DDX11L1 WASH7P
# 1      GTEX-N7MS-0007-SM-2D7W1 Blood       0    158
# 2      GTEX-N7MS-0008-SM-4E3JI  Skin       0    166
# 3 GTEX-N7MS-0011-R10A-SM-2HMJK Brain       0    209

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