0

I have a list of 6 sublists, each sublist contains 3 dataframes.

I want to set the row names of the dataframes to a column in that datagram.

list_1 <- list(df1, df2, df3)
list_2 <- lapply(list_1, function(x) split(x, x$Type))

I try to rename the rows of the data frames with lapply

lapply(list_2, function(x) column_to_rownames(x, var=x$Rows))

print(df1)
      Rows       A
1    Baseline    4
2    Sample1     5
3    Sample2     8
4    Sample3     6
5    AASHTO      9
6    Mean        3

print(df2)
      Rows       A
1    Baseline    4
2    Sample1     7
3    Sample2     8
4    Sample3     6
5    AASHTO      4
6    Mean        3

print(df3)
      Rows       A
1    Baseline    3
2    Sample1     5
3    Sample2     6
4    Sample3     6
5    AASHTO      5
6    Mean        3
Maral Dorri
  • 468
  • 5
  • 17

1 Answers1

1

You have nested list in list_2. Try unlisting one level

list_3 <- lapply(unlist(list_2, recursive = FALSE), function(x) 
               tibble::column_to_rownames(x, var= "Rows"))

Or using only base R :

list_3 <- lapply(unlist(list_2, recursive = FALSE), function(x) 
                 {rownames(x) <- x$Rows;x$Rows <- NULL;x})
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213