Home > front end >  rownames on multiple dataframe with for loop in R
rownames on multiple dataframe with for loop in R

Time:12-12

I have several dataframe. I want the first column to be the name of each row.

I can do it for 1 dataframe this way :

# Rename the row according the value in the 1st column
row.names(df1) <- df1[,1]

# Remove the 1st column
df1 <- df1[,-1]

But I want to do that on several dataframe. I tried several strategies, including with assign and some get, but with no success. Here the two main ways I've tried :

# Getting a list of all my dataframes
my_df <- list.files(path="data")

# 1st strategy, adapting what works for 1 dataframe
for (i in 1:length(files_names)) {
  rownames(get(my_df[i])) <- get(my_df[[i]])[,1] # The problem seems to be in this line
  my_df[i] <- my_df[i][,-1]
}

# The error is  Could not find function 'get>-'

# 2nd strategy using assign()
for (i in 1:length(my_df)) {
  assign(rownames(get(my_df[[i]])), get(my_df[[i]])[,1]) # The problem seems to be in this line
  my_df[i] <- my_df[i][,-1]
}

# The error is : Error in assign(rownames(my_df[i]), get(my_df[[i]])[, 1]) : first argument incorrect

I really don't see what I missed. When I type get(my_df[i]) and get(my_df[[i]])[,1], it works alone in the console...

Thank you very much to those who can help me :)

CodePudding user response:

We can use a loop function like lapply or purrr::map to loop through all the data.frames, then use dplyr::column_to_rownames, which simplifies the procedure a lot. No need for an explicit for loop.

library(purrr)
library(dplyr)

map(my_df, ~ .x %>% column_to_rownames(var = names(.)[1]))

CodePudding user response:

You may write the code that you have in a function, read the data and pass every dataframe to the function.

change_rownames <- function(df1) {
  row.names(df1) <- df1[,1]
  df1 <- df1[,-1]
  df1  
}

my_df <- list.files(path="data")

list_data <- lapply(my_df, function(x) change_rownames(read.csv(x)))
  • Related