0

I have a problem with changing names of columns of data frames which are in my list - I have list of df's which different number of rows but I think it's not important in what I want to do. I'd like to change the names of specified columns by adding name of data frame to them, ok enough talking, there's what I want to obtain:

1. Let's create some fake data:

    a <- c(1,2,3,4)
    b <- runif(4)
    c <- runif(4)
    d <- runif(4)
    df1 <- data.frame(a,b,c,d)
    df2 <- data.frame(a,b,c,d)
    df3 <- data.frame(a,b,c,d)
    my_list <- list(df1=df1, df2=df2)

I create those vectors three times (befeore creating df) to get different values, so as a result I have:

$df1
  a         b          c           d
1 1 0.6351811 0.51084221 0.038293376
2 2 0.9841223 0.15052422 0.877096060
3 3 0.3502053 0.01400321 0.231091711
4 4 0.8116008 0.46730937 0.006989978

$df2
  a          b         c         d
1 1 0.08205446 0.9186334 0.3346115
2 2 0.39882413 0.9171668 0.7663191
3 3 0.93652356 0.5607171 0.5846735
4 4 0.62940152 0.6683307 0.1347783

$df3
  a         b          c         d
1 1 0.6447916 0.83892606 0.4874532
2 2 0.6116721 0.09312807 0.9374633
3 3 0.3386178 0.29927137 0.5407353
4 4 0.5044569 0.81893434 0.8775277

Now what I want to get:

> my_list
$df1
  a     b_df1      c_df1     d_df1
1 1 0.6447916 0.83892606 0.4874532
2 2 0.6116721 0.09312807 0.9374633
3 3 0.3386178 0.29927137 0.5407353
4 4 0.5044569 0.81893434 0.8775277

$df2
  a     b_df2      c_df2     d_df2
1 1 0.6447916 0.83892606 0.4874532
2 2 0.6116721 0.09312807 0.9374633
3 3 0.3386178 0.29927137 0.5407353
4 4 0.5044569 0.81893434 0.8775277

$df3
  a     b_df3      c_df3     d_df3
1 1 0.6447916 0.83892606 0.4874532
2 2 0.6116721 0.09312807 0.9374633
3 3 0.3386178 0.29927137 0.5407353
4 4 0.5044569 0.81893434 0.8775277

What I did so far?

Of course I can do something like:

colnames(my_list[[1]]) <- c('a', paste(colnames(my_list[[1]])[2:4],'_','df1', sep=''))
colnames(my_list[[2]]) <- c('a', paste(colnames(my_list[[2]])[2:4],'_','df2', sep=''))
colnames(my_list[[3]]) <- c('a', paste(colnames(my_list[[3]])[2:4],'_','df3', sep=''))

But I'd like to use lapply() because my data is a list with plenty of data frames inside so I made something like:

tmp <- names(my_list)
lapply(tmp, function(x) colnames(my_list[[x]]) <- c('GENE_SYMBOL', paste(colnames(my_list[[x]])[2:4],'_',x, sep='')))

Unfortunately the result is:

[[1]]
[1] "GENE_SYMBOL" "b_df1"       "c_df1"       "d_df1"      

[[2]]
[1] "GENE_SYMBOL" "b_df2"       "c_df2"       "d_df2"      

[[3]]
[1] "GENE_SYMBOL" "b_df3"       "c_df3"       "d_df3"

Close but incorrect, no idea why :(

I think that isn't duplicate of this question because here my colnames are based on name of data frame...

Adamm
  • 2,150
  • 22
  • 30
  • How about `setNames(lapply(tmp, function(x) colnames(my_list[[x]]) <- c('GENE_SYMBOL', paste(colnames(my_list[[x]])[2:4],'_',x, sep=''))), tmp)`? – Axeman Jan 18 '18 at 08:04
  • 1
    @Axeman. You're right. It's a bit different. – patL Jan 18 '18 at 08:07

2 Answers2

1

You've almost done!. Just transform your loop into lapply this way:

lapply(seq_along(my_list), function(i)
  colnames(my_list[[i]]) <-
    c('GENE_SYMBOL', paste0(colnames(my_list[[i]])[2:4], '_', tmp[i])))

And using paste0 because you don't need separator.

Forge
  • 1,587
  • 1
  • 15
  • 36
0

I said that I'd like to use lapply() however...

tmp <- names(my_list)
for (i in seq_along(my_list)){
  colnames(my_list[[i]]) <- c('GENE_SYMBOL', paste(colnames(my_list[[i]])[2:4],'_',tmp[i], sep=''))
}
my_list
>my_list

$df1
  GENE_SYMBOL     b_df1      c_df1     d_df1
1           1 0.6447916 0.83892606 0.4874532
2           2 0.6116721 0.09312807 0.9374633
3           3 0.3386178 0.29927137 0.5407353
4           4 0.5044569 0.81893434 0.8775277

$df2
  GENE_SYMBOL     b_df2      c_df2     d_df2
1           1 0.6447916 0.83892606 0.4874532
2           2 0.6116721 0.09312807 0.9374633
3           3 0.3386178 0.29927137 0.5407353
4           4 0.5044569 0.81893434 0.8775277

$df3
  GENE_SYMBOL     b_df3      c_df3     d_df3
1           1 0.6447916 0.83892606 0.4874532
2           2 0.6116721 0.09312807 0.9374633
3           3 0.3386178 0.29927137 0.5407353
4           4 0.5044569 0.81893434 0.8775277

Thanks...

Adamm
  • 2,150
  • 22
  • 30