0

I have a list of dataframes and I want to assign the same column as the row names so when I print the dataframe, numbers do not appear. When I print out one of the dataframes in the list, I get the following result:

list1[["var1"]]
                               column1                                          NA
1                        Variable Name                                       value
2          Variable Collected at Waves                                       value
3           Variable Short Description                                       value 
4            Method of Data Collection                                       value 
5                   Variable Data Type                                       value 
6                  Variable SAS Format                                       value 
7                     Allowable Values                                       value  
8 Possible range of non-missing values                                       value 
9        Codes used for missing values                                       value 

I want the numbers on the left to be replaced by column 1 but applied to all lists entries. For example, it should look like this:

list1[["var1"]]
                                                                               NA
                         Variable Name                                      value
          Variable Collected at Waves                                       value
           Variable Short Description                                       value 
            Method of Data Collection                                       value 
                  Variable Data Type                                        value 
                  Variable SAS Format                                       value 
                     Allowable Values                                       value  
 Possible range of non-missing values                                       value 
        Codes used for missing values                                       value 

My best stab at it was here:

row.names(list1) <- lapply(list1, function(x) x['column1', ])
Andrew
  • 87
  • 7
  • 1
    Please provide a reproducible example See [mcve]. – andrew_reece Nov 06 '20 at 22:47
  • 1
    Why not use the row.names = FALSE option on print? `print(list1, row.names = FALSE)` – Reeza Nov 06 '20 at 23:04
  • https://stackoverflow.com/questions/24428051/removing-display-of-row-names-from-data-frame – Reeza Nov 06 '20 at 23:04
  • The issue is that the dataframe is in a list. When I try and do the following, it does not work: list1 = list1 %>% lapply(function(x) {rownames(x) <- NULL}). Also I am printing the list of dataframes using rmarkdown and the row.names = NA command within kable is not working which is why im trying to change the column to the row names. – Andrew Nov 06 '20 at 23:09
  • Unfortunately, because `print(..., row.names=)` is a function-argument and does not provide a way with `options` to do the same, you'll need to override `base::print.default` so that when it sub-calls `base::print.data.frame` for each element of your list-of-frames, it'll call with that argument. Not an answer, not tested, just offering that R does not make that an easy option for you. – r2evans Nov 06 '20 at 23:22

2 Answers2

0

this could solve your question:

library(dplyr)
library(textshape)
library(purrr)
# demo data.frame and list from this
df <- data.frame(value = c("***","*"),
                 nr = c(1,2))
l[[1]] <- df
l[[2]] <- df

# map colum to name function (per index only from what it appears)
purrr::map(l, function(x) x %>% textshape::column_to_rownames(1))

#result
[[1]]
    nr
***  1
*    2

[[2]]
    nr
***  1
*    2
DPH
  • 4,244
  • 1
  • 8
  • 18
0

You need to change the rownames of each element in the list. Try :

list2 <- lapply(list1, function(x) {
             #Assign rownames as 1st column
             rownames(x) <- x[[1]]
             #Remove 1st column.
             x <- x[-1]
             x
          })
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • I believe this answer along with the other answer will work but I keep getting an error saying "Error in `.rowNamesDF<-`(x, value = value) : duplicate 'row.names' are not allowed In addition: Warning message: Error in `.rowNamesDF<-`(x, value = value) : duplicate 'row.names' are not allowed" – Andrew Nov 09 '20 at 14:49
  • It means the first column in your data is not unique and have duplicate values in it. – Ronak Shah Nov 09 '20 at 14:57