Reputation: 2409
I have the following subset of two long data.frame
:
df1 <- data.frame("trial" = c('SA1','SA2','ES1','SA3','SA4','ES5'), "Genotype" = c('GID1','GID1','GID1','GID2','GID2','GID2'))
df2 <- data.frame('GID1'= c('0','1'),'GID2'=c('1','0'))
What I would like to do is, I want to index df1$Genotype
column by df2
colnames and expand df2
columns by substituting the values of df1$trial
as new columns in df2
.
So as a result I would have df3
as an expansion of df2
as follows :
df3 <- data.frame('SA1'=c(0,1), 'SA2'=c(0,1), 'ES1'=c(0,1), 'SA3'= c(1,0),'SA4'=c(1,0),'ES5'=c(1,0))
Please note the data.frame
is huge and I've tried functions like match
and %in%
without success.
Thanks in advance.
Upvotes: 1
Views: 47
Reputation: 48251
It's easier than it seems:
df3 <- df2[df1$Genotype]
colnames(df3) <- df1$trial
df3
# SA1 SA2 ES1 SA3 SA4 ES5
# 1 0 0 0 1 1 1
# 2 1 1 1 0 0 0
All we need to do is to select correct columns from df1
for each row of df1
, and my first line does exactly that.
Upvotes: 1