Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add character strings in numeric dataframes and concatenate two string dataframes [duplicate]

Tags:

r

I have got two numeric dataframes, df1 with log fold changes (LFC) and df2 with p-values.

df1 = data.frame(V1=c(1.2, 1.5, 0.3),
                 V2=c(0.5, 0.9, 1.1),
                 V3=c(-0.9, -1.5, -0.4))

df2 = data.frame(Y1=c(0.02, 0.005, 0.06),
                 Y2=c(0.05, 0.009, 0.01),
                 Y3=c(0.01, 0.001, 0.1))

and for some presentation purposes, I would like to add the prefix "LFC = " in df1, and the prefix "p = " in df2, such that I get the following:

df1

V1           V2           V3
LFC = 1.2    LFC = 0.5    LFC = -0.9
LFC = 1.5,   LFC = 0.9    LFC = -1.5
LFC = 0.3    LFC = 1.1    LFC = -0.4


df2

Y1           Y2            Y3
p = 0.02     p = 0.05      p = 0.01
p = 0.005    p = 0.009     p = 0.001
p = 0.06     p = 0.01      p = 0.1

and ultimately concatenate them to obtain a 3x3 dataframe structured like this:

df3

V1           V2           V3
LFC = 1.2,   LFC = 0.5,   LFC = -0.9,
p = 0.02     p = 0.05     p = 0.01

LFC = 1.5,   LFC = 0.9,   LFC = -1.5
p = 0.005    p = 0.009    p = 0.001

LFC = 0.3,   LFC = 1.1,   LFC = -0.4,
p = 0.06     p = 0.01     p = 0.1

What to do here? I appreciate your solutions.

like image 402
Nimzo Avatar asked Jul 11 '26 16:07

Nimzo


1 Answers

One way is to paste the prefixes and Map, i.e.

Map(`rbind`, lapply(df1, function(i)paste0('LFC = ', i)), 
             lapply(df2, function(i) paste0('p = ', i)))

which gives,

$V1
     [,1]        [,2]        [,3]       
[1,] "LFC = 1.2" "LFC = 1.5" "LFC = 0.3"
[2,] "p = 0.02"  "p = 0.005" "p = 0.06" 

$V2
     [,1]        [,2]        [,3]       
[1,] "LFC = 0.5" "LFC = 0.9" "LFC = 1.1"
[2,] "p = 0.05"  "p = 0.009" "p = 0.01" 

$V3
     [,1]         [,2]         [,3]        
[1,] "LFC = -0.9" "LFC = -1.5" "LFC = -0.4"
[2,] "p = 0.01"   "p = 0.001"  "p = 0.1"   

If you want a matrix (or data frame) rather than list, then simply use mapply instead of Map, i.e.

mapply(`rbind`, lapply(df1, function(i)paste0('LFC = ', i)), 
                lapply(df2, function(i) paste0('p = ', i)))

      V1          V2          V3          
[1,] "LFC = 1.2" "LFC = 0.5" "LFC = -0.9"
[2,] "p = 0.02"  "p = 0.05"  "p = 0.01"  
[3,] "LFC = 1.5" "LFC = 0.9" "LFC = -1.5"
[4,] "p = 0.005" "p = 0.009" "p = 0.001" 
[5,] "LFC = 0.3" "LFC = 1.1" "LFC = -0.4"
[6,] "p = 0.06"  "p = 0.01"  "p = 0.1"

Also instead of rbind you can paste, i.e.

 mapply(`paste`, lapply(df1, function(i)paste0('LFC = ', i)), 
                 lapply(df2, function(i) paste0('p = ', i)))

     V1                    V2                    V3                    
[1,] "LFC = 1.2 p = 0.02"  "LFC = 0.5 p = 0.05"  "LFC = -0.9 p = 0.01" 
[2,] "LFC = 1.5 p = 0.005" "LFC = 0.9 p = 0.009" "LFC = -1.5 p = 0.001"
[3,] "LFC = 0.3 p = 0.06"  "LFC = 1.1 p = 0.01"  "LFC = -0.4 p = 0.1"
like image 95
Sotos Avatar answered Jul 14 '26 04:07

Sotos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!