I am working with a csv file that I have loaded as a dataframe. It has 21,941 columns and is too large to open in Excel (which is how I would have normally done this). I need to rename all of these columns by assigning them IDs, which are just numbers from 1-21,941. So, 1, 2, 3, etc. The current names are all very long strings.
The first column is also blank, as that's where the rownames are. I don't know if the dataframe function takes this into account or not.
I have tried a few different things, with my current code being something like this:
library(dplyr)
data2 <- read.csv(file_name)
data2[,2:] <- rename_with(+1)
I know the +1
in the function is not correct and am unsure if I need to specify that it should not include the column where the rownames are. I've tried figuring out a function for the rename_with
, but all of the examples and explanations I've seen have included making a vector, and then creating a vec <- c(vec + 1)
kind of function to adapt it. However, there is no way I can manually list out every single column name for that, and am unsure if there is a function to do so or a better method that I don't know.
You can read in the data with the "row.names" specified as the first column. Then assign unique IDs to the names.
Example:
write.csv(mtcars, file="cars.csv")
data2 <- read.csv("cars.csv", row.names=1)
names(data2) <- seq_along(names(data2))
head(data2)
1 2 3 4 5 6 7 8 9 10 11
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1