【问题标题】:Rename list items重命名列表项
【发布时间】:2017-04-29 14:30:15
【问题描述】:

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

返回:

$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

如何将其元素重命名为201420152016

【问题讨论】:

  • 注意,listaValores 是一个列表。因此它的插槽名称不是colnames

标签: r list data-structures rename


【解决方案1】:

请注意,您有一个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

要仅编辑列表中的某些条目,请指定索引:

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

l

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

如果您想了解更多关于 R 中不同数据类型的信息,我可以推荐Hadley Wickham's summary

【讨论】:

  • 这对 OP 来说是一个很好的答案。不过,我想知道是否有类似于dplyr::rename() 的“tidyverse”解决方案适用于列表项。
  • 我认为这会很困难,因为它们都被命名为col1,这肯定会导致tidy-select 出现问题,它用于在dplyr::rename() 中查找.data 形式的列.
【解决方案2】:

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

#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

您还可以将 mylist 中的名称从列表名称 Col1 all 更改为相同的 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

【讨论】:

    【解决方案3】:

    一直在寻找相同的方法,以下可能也可以。

    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
    

    【讨论】:

      猜你喜欢
      • 2019-04-27
      • 2021-04-17
      • 2018-11-16
      • 1970-01-01
      • 2015-04-13
      • 2011-05-31
      • 2013-06-11
      • 2015-12-09
      相关资源
      最近更新 更多