Question: Making a data table more comprehensive
1
gravatar for Parham
23 months ago by
Parham1.3k
Sweden
Parham1.3k wrote:

Hi, I want to make a data table that I have, to something that is more comprehensive. In abstract this is what I intend to do:

> df
  cond_1 cond_2 cond_3 cond_4
1      a      a      b      e
2      b      b      c      f
3   <NA>      c      d      g

> df_t
  cond_1 cond_2 cond_3 cond_4
a      +      +      -      -
b      +      +      +      -
c      -      +      +      -
d      -      -      +      -
e      -      -      -      +
f      -      -      -      +
g      -      -      -      +

Is there any package or function that can do the job or facilitate coding? Any suggestion is a big help!

transform data.frame R • 605 views
ADD COMMENTlink modified 23 months ago by mikhail.shugay3.3k • written 23 months ago by Parham1.3k
2
gravatar for Santosh Anand
23 months ago by
Santosh Anand4.0k
Santosh Anand4.0k wrote:
rownames = unique(unlist(df))
rownames = rownames[!is.na(rownames)]
df2 = sapply(df, function(x) ifelse(rownames %in% x,  "+", "-"))
df3 = as.data.frame(df2, stringsAsFactors = F, row.names = rownames)

> df3
  cond_1 cond_2 cond_3 cond_4
a      +      +      -      -
b      +      +      +      -
c      -      +      +      -
d      -      -      +      -
e      -      -      -      +
f      -      -      -      +
g      -      -      -      +
ADD COMMENTlink written 23 months ago by Santosh Anand4.0k

Thank you all for your great and instructive solutions!

ADD REPLYlink written 23 months ago by Parham1.3k
1
gravatar for Steven Lakin
23 months ago by
Steven Lakin1.4k
Fort Collins, CO, USA
Steven Lakin1.4k wrote:
df <- data.frame(cond_1=c('a', 'b', NA),
             cond_2=c('a', 'b', 'c'),
             cond_3=c('b', 'c', 'd'),
             cond_4=c('e', 'f', 'g'))

df2 <- t(sapply(as.character(unique(unlist(df))), function(y) {
             lapply(df, function(x) {y %in% x})
        }))

df_t <- df2[!is.na(rownames(df2)), ]

# You can replace logicals like so

df_t[df_t == TRUE] <- '+'
df_t[df_t == FALSE] <- '-'

> df_t
cond_1 cond_2 cond_3 cond_4
a "+"    "+"    "-"    "-"   
b "+"    "+"    "+"    "-"   
c "-"    "+"    "+"    "-"   
d "-"    "-"    "+"    "-"   
e "-"    "-"    "-"    "+"   
f "-"    "-"    "-"    "+"   
g "-"    "-"    "-"    "+"
ADD COMMENTlink modified 23 months ago • written 23 months ago by Steven Lakin1.4k
1
gravatar for mikhail.shugay
23 months ago by
mikhail.shugay3.3k
Czech Republic, Brno, CEITEC
mikhail.shugay3.3k wrote:

Here you go:

df <- data.frame(cond_1=c('a', 'b', NA),
                 cond_2=c('a', 'b', 'c'),
                 cond_3=c('b', 'c', 'd'),
                 cond_4=c('e', 'f', 'g'))

library(reshape2)
df <- subset(dcast(melt(df, id.vars = c()), value~variable), !is.na(value))
rownames(df) <- df$value
df$value <- NULL

ifelse( is.na(df), "-", "+")
ADD COMMENTlink modified 23 months ago • written 23 months ago by mikhail.shugay3.3k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1132 users visited in the last hour