简体   繁体   English

向 R 中的向量列表中的每个向量添加一个字符

[英]Add a character to each vector in a list of vectors in R

I have a list of character vectors:我有一个字符向量列表:

vector.list <- list("cytidine", "uridine", "dihydrouridine", "2'-O-methylcytidine")

I want to add a the character "30" to each vector in this list, resulting in:我想在这个列表中的每个向量中添加一个字符“30”,结果是:

vector.list
[[1]]
[1] "30"       "cytidine"

[[2]]
[1] "30"      "uridine"

[[3]]
[1] "30"             "dihydrouridine"

[[4]]
[1] "30"                  "2'-O-methylcytidine

I know how to do this with a "for loop" or by writing a function and using lapply.我知道如何使用“for 循环”或通过编写函数并使用 lapply 来做到这一点。 For example:例如:

for (i in 1:length(vector.list)){
vector.list[[i]] <- c("30", vector.list[[i]])
}

However, I need to do this for many combinations of vector lists and characters and I want this code to be accessible to other people.但是,我需要对矢量列表和字符的许多组合执行此操作,并且我希望其他人可以访问此代码。 I was wondering if there was a more elegant command for adding a character to each vector in a list of vectors in R?我想知道是否有更优雅的命令可以为 R 中的向量列表中的每个向量添加一个字符?

lapply(vector.list, append, "30", after=0)
lapply(vector.list,
       function(x) c("30", x))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM