如何使用 R 中的 apply 将字符串粘贴到字符串向量的每个元素上?

发布于 2024-11-28 09:06:32 字数 215 浏览 1 评论 0原文

我有一个字符串向量。

d <- c("Mon","Tues","Wednes","Thurs","Fri","Satur","Sun")

为此,我想以与此类似的方式将字符串“day”粘贴到向量的每个元素上。

week <- apply(d, "day", paste, sep='')

I have a vector of strings.

d <- c("Mon","Tues","Wednes","Thurs","Fri","Satur","Sun")

for which I want to paste the string "day" on each element of the vector in a way similar to this.

week <- apply(d, "day", paste, sep='')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

↘人皮目录ツ 2024-12-05 09:06:32

不需要apply(),只需使用paste()

R> d <- c("Mon","Tues","Wednes","Thurs","Fri","Satur","Sun")
R> week <- paste(d, "day", sep="")
R> week
[1] "Monday"    "Tuesday"   "Wednesday" "Thursday"  
[4] "Friday"    "Saturday"  "Sunday"   
R> 

No need for apply(), just use paste():

R> d <- c("Mon","Tues","Wednes","Thurs","Fri","Satur","Sun")
R> week <- paste(d, "day", sep="")
R> week
[1] "Monday"    "Tuesday"   "Wednesday" "Thursday"  
[4] "Friday"    "Saturday"  "Sunday"   
R> 
金兰素衣 2024-12-05 09:06:32

其他人已经指出,由于 paste 是矢量化的,因此在这种情况下不需要使用 apply

但是,回答您的问题: apply 用于数组或 data.frame。当您想要对列表(或向量)应用函数时,请使用 lapplysapplylapply 的变体,简化了结果):

sapply(d, paste, "day", sep="")
        Mon        Tues      Wednes       Thurs         Fri       Satur 
   "Monday"   "Tuesday" "Wednesday"  "Thursday"    "Friday"  "Saturday" 
        Sun 
   "Sunday" 

Other have already indicated that since paste is vectorised, there is no need to use apply in this case.

However, to answer your question: apply is used for an array or data.frame. When you want to apply a function over a list (or a vector) then use lapply or sapply (a variant of lapply that simplifies the results):

sapply(d, paste, "day", sep="")
        Mon        Tues      Wednes       Thurs         Fri       Satur 
   "Monday"   "Tuesday" "Wednesday"  "Thursday"    "Friday"  "Saturday" 
        Sun 
   "Sunday" 
三人与歌 2024-12-05 09:06:32

除了 paste/paste0 之外,我们还可以通过多种方式向向量中的每个元素添加字符串。

1) 使用 sprintf

sprintf("%sday", d)
#[1] "Monday"    "Tuesday" "Wednesday" "Thursday"  "Friday"  "Saturday"  "Sunday" 

2) glue

glue::glue("{d}days")

这里 {d} 被评估为 R 代码。如果需要,可以将其包装在 as.character 中。

3) stringr 中的 str_c

stringr::str_c(d, "day")

其等效项为

4) stringi 中的 stri_c

stringi::stri_c(d, "day")

5) stringi > 还有 stri_paste

stringi::stri_paste(d, "day")

Apart from paste/paste0 there are variety of ways in which we can add a string to every element in the vector.

1) Using sprintf

sprintf("%sday", d)
#[1] "Monday"    "Tuesday" "Wednesday" "Thursday"  "Friday"  "Saturday"  "Sunday" 

2) glue

glue::glue("{d}days")

Here {d} is evaluated as R code. This can be wrapped in as.character if needed.

3) str_c in stringr

stringr::str_c(d, "day")

whose equivalent is

4) stri_c in stringi

stringi::stri_c(d, "day")

5) stringi also has stri_paste

stringi::stri_paste(d, "day")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文