list 重命名列表项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41139570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rename list items
提问by Iván Rodas Padilla
I have the following list listaValores
我有以下清单 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
?
如何将其元素重命名为2014
, 2015
, 2016
?
回答by loki
Note that you have a list
. Therefore, you do not have colnames
but names
. You can edit them like this:
请注意,您有一个list
. 因此,您没有colnames
但是names
。您可以像这样编辑它们:
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.
如果您想了解更多有关 R 中不同数据类型的信息,我可以推荐Hadley Wickham 的摘要。