R how to change dafaframe column name using listname

I have a list y as shown below. I want to change the first column name of each dataframe (rn) to the name of the dataframe (SA, TA). So it will look like y1.

> y
$SA
          rn  X1   X2   X3  X4   X5  X6
1: timepoint   0 3.75 4.25 4.5 4.75   5
2:      plot 234  304  285 279  256 238

$TA
          rn  X7  X8   X9  X10  X11 X12
1: timepoint   0   5 4.25 3.75 4.75 4.5
2:      plot 208 299  272  261  254 218

> y1
$SA
          SA  X1   X2   X3  X4   X5  X6
1: timepoint   0 3.75 4.25 4.5 4.75   5
2:      plot 234  304  285 279  256 238

$TA
          TA  X7  X8   X9  X10  X11 X12
1: timepoint   0   5 4.25 3.75 4.75 4.5
2:      plot 208 299  272  261  254 218

      

+3


source to share


1 answer


These list items look like data tables, so they should be as simple as

y1 <- Map(setnames, y, "rn", names(y))

      



Replace "rn"

with 1

if you want to index by the first column and not by the column name "rn".

+2


source







All Articles