shams
shams

Reputation: 162

map respective values in arrayed r data frame

is there any way to get the respective values from columns of an arrayed data frame in which the order of reference is described in one.

Example data

data.frame(ref="ENSG00000272602.1;ENSG00000272602.4;ENSG00000272602.2" , b="0;1;0", c="1;3;0",  d="0;1;7", e="0;3.2;2")

                                                 ref    b     c      d   e 
ENSG00000272602.1;ENSG00000272602.4;ENSG00000272602.2 0;1;0 1;3;0 0;1;7  0;3.2;2

desired output

                ref b c d e
1 ENSG00000272602.4 1 3 1 3.2

Upvotes: 1

Views: 47

Answers (3)

Ronak Shah
Ronak Shah

Reputation: 388862

Using data.table, we can split the string on ";", create new rows and then filter row based on target.

library(data.table)
target <- "ENSG00000272602.4"

setDT(df)
df[,lapply(.SD, function(x) unlist(tstrsplit(x, ";", fixed=TRUE)))][ref == target]

#                 ref b c d   e
#1: ENSG00000272602.4 1 3 1 3.2

Upvotes: 1

akrun
akrun

Reputation: 887008

We can split at the ; with cSplit into 'long' format and use filter_at to get the rows with any non-zero values

library(splitstackshape)
library(dplyr)
cSplit(d1, names(d1), ";", "long") %>%
         filter_at(-1,  any_vars(. > 0))
#                ref b c d   e
#1 ENSG00000272602.4 1 3 1 3.2

Or using separate_rows

library(tidyr)
d1 %>%
     separate_rows(everything(), sep= ";") %>% 
     filter_at(-1,  any_vars(. > 0))
#               ref b c d   e
#1 ENSG00000272602.4 1 3 1 3.2

data

d1 <- data.frame(ref="ENSG00000272602.1;ENSG00000272602.4;ENSG00000272602.2" , b="0;1;0", c="1;3;0",  d="0;1;7", e="0;3.2;2")

Upvotes: 2

andrew_reece
andrew_reece

Reputation: 21264

You can use separate_rows():

library(tidyverse)

target <- "ENSG00000272602.4"
df %>% 
  separate_rows(everything(), sep =  ";") %>%
  filter(ref == target)

                ref b c d   e
1 ENSG00000272602.4 1 3 1 3.2

Upvotes: 1

Related Questions