Extract and assign data information by matching columns of two data frames
1
0
Entering edit mode
19 months ago
Bio • 0

Hi there, I would like to extract and assign study information to the study column of my.data from info.data by matching their samples column.

I did the following:

my.data <- matrix(1:18, nrow = 6, dimnames = list(c("X","Y","Z","A","G","C"), c("A","B","C")))



info.data <- data.frame(samples = rep(rep(c("A","B","C")), times=1),
                        study = rep(rep(c("S1","S2","S3")), times=1))


as.data.frame(my.data)  %>%   
      rownames_to_column(var = "row") %>%  
      gather(key = 'samples', value = 'rep', -row) %>%   mutate(study =
      info.data$study[samples %in% info.data$samples])

But it fills out the first three rows of the study column and in an incorrect way.

Any help much appreciated!

R dplyr • 785 views
ADD COMMENT
1
Entering edit mode
19 months ago
my.data |>
  as_tibble(rownames="row") |>
  pivot_longer(!row, names_to="samples", values_to="rep") |>
  left_join(info.data)
ADD COMMENT
0
Entering edit mode

Thanks a lot for your help

ADD REPLY
0
Entering edit mode

Is there any other solution in the case when info.data contain many other columns?

ADD REPLY
0
Entering edit mode

do you want to include or exclude those columns?

ADD REPLY
0
Entering edit mode

not really, I need only study column

ADD REPLY
1
Entering edit mode

left_join(select(info.data, samples, study))

ADD REPLY

Login before adding your answer.

Traffic: 1262 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6