Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 205491

Changing a Single Column Across Multiple Data Frames in a List

$
0
0

These questions:

Changing Column Names in a List of Data Frames in R

Changing names in a list of dataframes

Both have great solutions to changing column names, but the only column name I want to change is the first column. The data frames in my list only have the first column in common.

Here is a reproducible example of the issue that I'm having:

df1 <- data.frame(A = 1:5, B = 1:5)
df2 <- data.frame(A = 11:15, B = 21:25)
ldf <- list(df1, df2)
ldf <- lapply(ldf, setNames, "State")
L
[[1]]
  State NA
1     1  1
2     2  2
3     3  3
4     4  4
5     5  5

[[2]]
  State NA
1    11 21
2    12 22
3    13 23
4    14 24
5    15 25

How do I specify a single column in lapply and leave the rest alone?


Viewing all articles
Browse latest Browse all 205491

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>