26

I have the following list listaValores

listaValores <- c()
  for(valores in 1:numRepeticion){
    listaValores <- c(listaValores, readWorksheetFromFile(file = file.read,        
                        sheet = sheet.read, 
                        startRow = startRow.read+(12*(valores-1)),
                        startCol = startCol.read[i], 
                        endRow = startRow.read+((12*valores)-1) ,
                        endCol = startCol.read[i], header = FALSE))  
    }

which returns:

$Col1
 [1] 32824 35646 34650 29328 27376 28548 35363 34740 49181 57960 55550 50626

$Col1
 [1] 52610 55085 58576 51300 50968 58104 56585 38273 54216 59043 67487 58067

$Col1
 [1] 59142 68593 77510 73434 83545 83483 79635 69269 85703 73080

How to renames it's elements to 2014, 2015, 2016?

loki
  • 9,816
  • 7
  • 56
  • 82

4 Answers4

48

Note that you have a list. Therefore, you do not have colnames but names. You can edit them like this:

l <- list(col1 = c(123123, 12123, 123123), col1 =  c(123123, 12123, 123123))
l 
# $col1
# [1] 123123  12123 123123
# 
# $col1
# [1] 123123  12123 123123

names(l)
# [1] "col1" "col1"

names(l) <- c("2014", "2015")

l

# $`2014`
# [1] 123123  12123 123123
# 
# $`2015`
# [1] 123123  12123 123123

To only edit certain entries in the list, specify an index:

names(l)[1] <- "new_name"

l

# $`new_name`
# [1] 123123  12123 123123
# 
# $`2015`
# [1] 123123  12123 123123

If you'd like to know more about the different data types in R, I can recommend Hadley Wickham's summary.

loki
  • 9,816
  • 7
  • 56
  • 82
  • This is a good answer for OP. I'm wondering, though, if there is a "tidyverse" solution akin to `dplyr::rename()` that works with list items. – FreyGeospatial Jan 19 '22 at 14:00
  • I figure this would be difficult as all of them are named `col1` and this would most certainly cause issues with `tidy-select`, which is used to find the columns form `.data` in `dplyr::rename()`. – loki Jan 19 '22 at 16:26
5

If you want to use the list name instead of the index, this works.

#Reproduce example list
mylist <- list(Col1 = c(32824, 35646, 34650, 29328, 27376, 28548, 35363, 34740, 49181, 57960, 55550, 50626), Col1 =  c(52610, 55085, 58576, 51300, 50968, 58104, 56585, 38273, 54216, 59043, 67487, 58067), Col1 =  c(59142, 68593, 77510, 73434, 83545, 83483, 79635, 69269, 85703, 73080))
mylist
$Col1
 [1] 32824 35646 34650 29328 27376 28548 35363 34740 49181 57960 55550 50626

$Col1
 [1] 52610 55085 58576 51300 50968 58104 56585 38273 54216 59043 67487 58067

$Col1
 [1] 59142 68593 77510 73434 83545 83483 79635 69269 85703 73080

#Change names in mylist from Col1 to 2014, 2015, 2016
names(mylist) <- c("2014", "2015", "2016")
mylist
$`2014`
 [1] 32824 35646 34650 29328 27376 28548 35363 34740 49181 57960 55550 50626

$`2015`
 [1] 52610 55085 58576 51300 50968 58104 56585 38273 54216 59043 67487 58067

$`2016`
 [1] 59142 68593 77510 73434 83545 83483 79635 69269 85703 73080

You can also change names in mylist from list name Col1 all to the same new.name.

names(mylist)[names(mylist) == "Col1"] <- "new.name"
mylist
$new.name
 [1] 32824 35646 34650 29328 27376 28548 35363 34740 49181 57960 55550 50626

$new.name
 [1] 52610 55085 58576 51300 50968 58104 56585 38273 54216 59043 67487 58067

$new.name
 [1] 59142 68593 77510 73434 83545 83483 79635 69269 85703 73080
simpson
  • 151
  • 1
  • 12
2

Since some users come here seeking a dplyr::rename() like solution and this question is being used as a target to close more general questions about renaming lists, here is a solution:

rename = function(.data, ...) {
    mapping = sapply(
        rlang::enquos(...),
        rlang::as_name
    )
    new_names = setNames(nm=names(.data))
    # `new_name = old_name` for consistency with `dplyr::rename`
    new_names[mapping] = names(mapping)
    # for `old_name = new_name` use: `new_names[names(mapping)] = mapping`
    setNames(.data, new_names)
}

Use it like:

my_list = list(a=1, b=2, c=3)
my_list |> rename(x=a, y=b)

or

my_list = list(a=1, b=2, c=3)
rename(my_list, x=a, y=b)
krassowski
  • 13,598
  • 4
  • 60
  • 92
0

Was looking for the same, the following may work as well.

l <- list(col1 = c(123123, 12123, 123123), col1 =  c(123123, 12123, 123123));l
names(l) <- paste("l", seq_along(l), sep = "");l
names(l) <- paste( c(2004:2005), sep = "");l
Seyma Kalay
  • 2,037
  • 10
  • 22