如何在r中命名列表的元素?
假设我有以下列表:如何在r中命名列表的元素?,r,R,假设我有以下列表: my_variable <- list() x <- c(1,2,3,4) y <- c(4,5,7,3) for ( i in 1:4){ my_variable[[i]] <- x[i]*y[i]+2 } 如何命名输出的元素,如下所示: > my_variable First_result [1] 6 Second_result [1] 12 依此类推。您可以使用paste0和names # So first you
my_variable <- list()
x <- c(1,2,3,4)
y <- c(4,5,7,3)
for ( i in 1:4){
my_variable[[i]] <- x[i]*y[i]+2
}
如何命名输出的元素,如下所示:
> my_variable
First_result
[1] 6
Second_result
[1] 12
依此类推。您可以使用
paste0
和names
# So first you define vector of names:
names1 <- c("First","Second","Third","Fourth")
# And second you paste them to your list
names(my_variable) <- paste0(names1,"_result", sep = "")
#And the output
$First_result
[1] 6 12 23 14
$Second_result
[1] 6 12 23 14
$Third_result
[1] 6 12 23 14
$Fourth_result
[1] 6 12 23 14
#首先定义名称向量:
名称1名称(我的变量)您可以使用名称(我的变量)
# So first you define vector of names:
names1 <- c("First","Second","Third","Fourth")
# And second you paste them to your list
names(my_variable) <- paste0(names1,"_result", sep = "")
#And the output
$First_result
[1] 6 12 23 14
$Second_result
[1] 6 12 23 14
$Third_result
[1] 6 12 23 14
$Fourth_result
[1] 6 12 23 14