'names' 属性 [4000] 必须与向量 [5] 的长度相同
'names' attribute [4000] must be the same length as the vector [5]
每当我尝试更改列表中数据框的名称时,我都会遇到错误。我认为这是由于我要更改的名称与数据框中的长度之间的长度不同。如果这似乎是重复发布,我深表歉意。如果是这样,我会删除,但如果有人能指出正确的方向,我将不胜感激。
library(lubridate)
library(tidyverse)
date <- rep_len(seq(dmy("01-01-2010"), dmy("31-12-2013"), by = "days"),1000)
ID <- rep(c("A","B","C", "D", "E"), 100)
df <- data.frame(date = date,
x = runif(length(date), min = 60000, max = 80000),
y = runif(length(date), min = 800000, max = 900000),
ID)
df1 <- df %>% group_split(ID)
l <- list(1,2,3,4,5)
我已经能够使用它来尝试更改它,但在尝试之后我得到了错误:
for(i in 1: length(df1)){
names(l) <- unlist(df1[[i]]$ID)
}
我正在尝试将这些值更改为 ID 值:
Error in names(l) <- unlist(df1[[i]]$ID) :
'names' attribute [200] must be the same length as the vector [5]
setNames
是你需要的:
setNames(l, unique(df$ID))
输出:
$A
[1] 1
$B
[1] 2
$C
[1] 3
$D
[1] 4
$E
[1] 5
每当我尝试更改列表中数据框的名称时,我都会遇到错误。我认为这是由于我要更改的名称与数据框中的长度之间的长度不同。如果这似乎是重复发布,我深表歉意。如果是这样,我会删除,但如果有人能指出正确的方向,我将不胜感激。
library(lubridate)
library(tidyverse)
date <- rep_len(seq(dmy("01-01-2010"), dmy("31-12-2013"), by = "days"),1000)
ID <- rep(c("A","B","C", "D", "E"), 100)
df <- data.frame(date = date,
x = runif(length(date), min = 60000, max = 80000),
y = runif(length(date), min = 800000, max = 900000),
ID)
df1 <- df %>% group_split(ID)
l <- list(1,2,3,4,5)
我已经能够使用它来尝试更改它,但在尝试之后我得到了错误:
for(i in 1: length(df1)){
names(l) <- unlist(df1[[i]]$ID)
}
我正在尝试将这些值更改为 ID 值:
Error in names(l) <- unlist(df1[[i]]$ID) :
'names' attribute [200] must be the same length as the vector [5]
setNames
是你需要的:
setNames(l, unique(df$ID))
输出:
$A
[1] 1
$B
[1] 2
$C
[1] 3
$D
[1] 4
$E
[1] 5