0

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 :)

aynber
  • 22,380
  • 8
  • 50
  • 63

2 Answers2

0

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 %>% read.csv() %>% column_to_rownames(var = names(.)[1]))
GuedesBF
  • 8,409
  • 5
  • 19
  • 37
0

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)))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • HI, thank you for your answer. I thaught to a fonction to, but it doesn't work. I did this, as you wrote (just use read.table instead of read.csv, but even as you wrote it doen't work) \n ```change_rownames <- function(df) { row.names(df) <- df[,1] df <- df[,-1] df } list_data <- lapply(files_names, function(x) change_rownames(read.table(x, header = F, sep = "\t"))) ``` – user1156574 Dec 14 '21 at 00:29