Hi,
Welcome to the RStudio community!
Here is an example on how you can clean your data and join it with new data
library(tidyverse)
myData = structure(c(0.6756, 2.0564, 0.990533333333333, 1.252, 0.996266666666667,
0.7107, 0.9596, 1.7188, 0.925, 1.2134, 1.06933333333333, 1.75393333333333,
0.936866666666667, 1.52126666666667, 0.341266666666667, 1.21433333333333,
1.09193333333333, 0.822666666666667, 1.40113333333333, 1.26673333333333,
1.31053333333333, 1.10506666666667, 1.06953333333333, 0.541766666666667,
0.815333333333333, 0.831466666666667, 0.4028, 1.25846666666667,
0.922066666666667, 0.6774, 0.964866666666667, 0.7709, 0.404933333333333,
1.19093333333333, 0.9622, 0.655766666666667, 0.772466666666667,
1.1494, 0.246266666666667, 1.2376, 1.10593333333333, 0.627466666666667,
0.902933333333333, 0.8008, 1.017, 1.21596666666667, 1.10786666666667,
0.6652, 0.8872, 0.783, 0.749933333333333, 1.27313333333333, 0.908533333333333,
0.6245, 0.659733333333333, 0.946666666666667, 1.77033333333333,
0.923133333333333, 1.01566666666667, 1.21173333333333, 0.6548,
1.39953333333333, 1.08426666666667, 0.827766666666667, 1.14753333333333,
0.851066666666667, 0.482133333333333, 0.978033333333333, 1.9255,
0.6996, 1.12153333333333, 1.00046666666667, 1.19473333333333,
0.576266666666667, 0.282266666666667, 1.31666666666667, 1.00833333333333,
1.02413333333333, 0.780933333333333, 1.15693333333333, 0.272533333333333,
1.17853333333333, 1.05426666666667, 0.518533333333333, 1.01946666666667,
0.8346, 0.5708, 1.49526666666667, 1.07893333333333, 0.673), .Dim = c(6L,
15L),
.Dimnames = list(c("DDR1 /// MIR4640", "RFC2", "HSPA6",
"PAX8", "GUCA1A", "MIR5193 /// UBA7"),
c("GSM647547", "GSM647552", "GSM647553", "GSM647565", "GSM647569",
"GSM647574", "GSM647577", "GSM647580", "GSM647560", "GSM647619",
"GSM647550", "GSM647528", "GSM647537", "GSM647616", "GSM647626")))
#Transform into data frame and clean genes
myData = myData %>% as.data.frame() %>% rownames_to_column("gene") %>%
rowwise() %>%
mutate(gene = str_split(gene, " /// ", simplify = T) %>%
sort() %>% paste(collapse = "_"))
myData[,1:3]
#> # A tibble: 6 x 3
#> # Rowwise:
#> gene GSM647547 GSM647552
#> <chr> <dbl> <dbl>
#> 1 DDR1_MIR4640 0.676 0.960
#> 2 RFC2 2.06 1.72
#> 3 HSPA6 0.991 0.925
#> 4 PAX8 1.25 1.21
#> 5 GUCA1A 0.996 1.07
#> 6 MIR5193_UBA7 0.711 1.75
#new data
newData = data.frame(
gene = c("PAX8", "MIR5193 ... UBA7", "HSPA6", "CFTR", "MIR4640 ... DDR1"),
newVal1 = runif(5),
newVal2 = runif(5)
)
newData
#> gene newVal1 newVal2
#> 1 PAX8 0.3042653 0.7143048
#> 2 MIR5193 ... UBA7 0.3860882 0.7852422
#> 3 HSPA6 0.6428005 0.7612599
#> 4 CFTR 0.2792017 0.5716758
#> 5 MIR4640 ... DDR1 0.9572771 0.2558074
#Transform to match the existing one
newData = newData %>% rowwise() %>%
mutate(gene = str_split(gene, " ... ", simplify = T) %>%
sort() %>% paste(collapse = "_"))
newData
#> # A tibble: 5 x 3
#> # Rowwise:
#> gene newVal1 newVal2
#> <chr> <dbl> <dbl>
#> 1 PAX8 0.304 0.714
#> 2 MIR5193_UBA7 0.386 0.785
#> 3 HSPA6 0.643 0.761
#> 4 CFTR 0.279 0.572
#> 5 DDR1_MIR4640 0.957 0.256
#Join
result = myData %>% full_join(newData, by = "gene")
#Result
result[c(1:2, 15:18)]
#> # A tibble: 7 x 6
#> # Rowwise:
#> gene GSM647547 GSM647616 GSM647626 newVal1 newVal2
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 DDR1_MIR4640 0.676 0.781 1.02 0.957 0.256
#> 2 RFC2 2.06 1.16 0.835 NA NA
#> 3 HSPA6 0.991 0.273 0.571 0.643 0.761
#> 4 PAX8 1.25 1.18 1.50 0.304 0.714
#> 5 GUCA1A 0.996 1.05 1.08 NA NA
#> 6 MIR5193_UBA7 0.711 0.519 0.673 0.386 0.785
#> 7 CFTR NA NA NA 0.279 0.572
Created on 2021-05-06 by the reprex package (v2.0.0)
By making sure you format the "gene" column the same for every dataset, you can then simply join by it. I did this by splitting the name of any column with multiple genes by its divider, then sorting it and pasting it back together with "_". This way you can transform any gene with multiple entries into a consistent one.
Hope this helps,
PJ