重新排列R中的数据以获取每组条件的变量值

时间:2022-11-09 13:08:21

I have a data set where each 'subject' with a particular 'stim' condition has a certain value for the variable 'FOXP3', 'GATA3' and 'GZMB'. I would like to group the 'subject' and 'stim' together to get the corresponding values for 'FOXP3', 'GATA3' and 'GZMB'.

我有一个数据集,其中具有特定“刺激”条件的每个“主体”对于变量“FOXP3”,“GATA3”和“GZMB”具有一定值。我想将'subject'和'stim'组合在一起,得到'FOXP3','GATA3'和'GZMB'的相应值。

So the data looks like this:

所以数据看起来像这样:

subject   stim    FOXP3  GATA3  GZMB
TA        no stim   0      0.50   0
SA        11002     1       0     0.9
RK        4512      0.3     0.5   0.9

I want the data to look like this

我希望数据看起来像这样

            TA+no stim      SA+11002       RK+4512   
FOXP3        0                 1             0.3
GATA3        0.5               0             0.5 
GZMB         0                 0.9           0.9

How do I do it?

我该怎么做?

2 个解决方案

#1


0  

This is how I would do it:

我就是这样做的:

library(dplyr)
library(tidyr)
df <- read.table(text = 
"subject   stim    FOXP3  GATA3  GZMB
TA        no-stim   0      0.50   0
SA        11002     1       0     0.9
RK        4512      0.3     0.5   0.9", header = T)
df2 <- df %>% unite(col = subject_stim, subject, stim, sep = "+") %>% 
{as_tibble(cbind(names = names(.), t(.)))} 
colnames(df2)[2:4] <- df2[1,2:4]
df3 <- df2[-1,]

It is not the prettiest but it works. The as_tibble(cbind(names = names(.), t(.))) comes from this question

它不是最漂亮但它有效。 as_tibble(cbind(names = names(。),t(。)))来自这个问题

#2


0  

Using data.table, you can do in two steps using melt and dcast:

使用data.table,您可以使用melt和dcast分两步完成:

## first melt
df1 <- melt(data = df, id.vars = c('subject','stim'))
df1$col_name = paste(df1$subject,df1$stim, sep = '+')

## then spread
df1 <- dcast(df1, variable ~ col_name, value.var = 'value')
print(df1)

     variable RK+4512 SA+11002 TA+no_stim
1:    FOXP3     0.3      1.0        0.0
2:    GATA3     0.5      0.0        0.5
3:     GZMB     0.9      0.9        0.0

#1


0  

This is how I would do it:

我就是这样做的:

library(dplyr)
library(tidyr)
df <- read.table(text = 
"subject   stim    FOXP3  GATA3  GZMB
TA        no-stim   0      0.50   0
SA        11002     1       0     0.9
RK        4512      0.3     0.5   0.9", header = T)
df2 <- df %>% unite(col = subject_stim, subject, stim, sep = "+") %>% 
{as_tibble(cbind(names = names(.), t(.)))} 
colnames(df2)[2:4] <- df2[1,2:4]
df3 <- df2[-1,]

It is not the prettiest but it works. The as_tibble(cbind(names = names(.), t(.))) comes from this question

它不是最漂亮但它有效。 as_tibble(cbind(names = names(。),t(。)))来自这个问题

#2


0  

Using data.table, you can do in two steps using melt and dcast:

使用data.table,您可以使用melt和dcast分两步完成:

## first melt
df1 <- melt(data = df, id.vars = c('subject','stim'))
df1$col_name = paste(df1$subject,df1$stim, sep = '+')

## then spread
df1 <- dcast(df1, variable ~ col_name, value.var = 'value')
print(df1)

     variable RK+4512 SA+11002 TA+no_stim
1:    FOXP3     0.3      1.0        0.0
2:    GATA3     0.5      0.0        0.5
3:     GZMB     0.9      0.9        0.0