R 重命名列表项

R 重命名列表项,r,list,data-structures,rename,R,List,Data Structures,Rename,我有以下列表listaValores listaValores <- c() for(valores in 1:numRepeticion){ listaValores <- c(listaValores, readWorksheetFromFile(file = file.read, sheet = sheet.read, startRow = startR

我有以下列表
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))  
    }

如何将其元素重命名为
2014
2015
2016

请注意,您有一个
列表。因此,您没有
colnames
,而是
名称
。您可以这样编辑它们:

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

l如果您想使用列表名而不是索引,这是可行的

#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
#复制示例列表

请注意,
listaValores
是一个列表。因此,它的插槽名称不是
colnames
names(l)[1] <- "new_name"

l

# $`new_name`
# [1] 123123  12123 123123
# 
# $`2015`
# [1] 123123  12123 123123
#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
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