簡體   English   中英

如何創建一個新的 function 來迭代我之前在 R 中創建的 function?

[英]How do I create a new function that iterates a function I previously made in R?

我必須創建 3 個函數來唱聖誕節的 12 天。 第一個 function 組成一個詞組。

make_phrase <- function(num, gift) {
  if (!is.character(gift[1])) {
    stop("Gift should be a string of characters") }
    if (!is.numeric(num[1])) {
    stop("Num should be a number") }
  num <- as.character(english(num))
  num <- ifelse(num == "one", "a", num)
  glue("{num} {gift}")
  }

第二個 function 使用第一個來創作歌曲的主旋律。

sing_verse <- function(num, day, gift) {
  day <- day[num]
  daily_phrase <- make_phrase(num:1, gift[num:1])
  cat("On the", day, "day of Christmas, my true love gave to me, \n")
      glue("{daily_phrase}")
}

這兩個功能似乎都有效,我想創建第三個 function,它使用 sing_verse function 來唱整首歌。這個 function 需要有三個 arguments。基本上這樣做:

sing_verse(1, xmas$day_in_words, xmas$gift_item)
sing_verse(2, xmas$day_in_words, xmas$gift_item)
sing_verse(3, xmas$day_in_words, xmas$gift_item)
sing_verse(4, xmas$day_in_words, xmas$gift_item)
sing_verse(5, xmas$day_in_words, xmas$gift_item)
sing_verse(6, xmas$day_in_words, xmas$gift_item)
sing_verse(7, xmas$day_in_words, xmas$gift_item)
sing_verse(8, xmas$day_in_words, xmas$gift_item)
sing_verse(9, xmas$day_in_words, xmas$gift_item)
sing_verse(10, xmas$day_in_words, xmas$gift_item)
sing_verse(11, xmas$day_in_words, xmas$gift_item)
sing_verse(12, xmas$day_in_words, xmas$gift_item)

我試過這個:

sing_xmas_song <- function(days, names, gifts) {
verses<- map_chr(days, ~sing_verse(.x, names, gifts)) %>%
  cat(sep = "\n")
return(verses)
}

但我收到一條錯誤消息,指出“錯誤:結果 2 必須是單個字符串,而不是 class glue/character且長度為 2 的向量”

如果我更改第二個 function 以在 cat() 中包含 glue() 它可以解決該問題,但我會收到一條錯誤消息,指出“錯誤:結果 1 必須是字符串,而不是長度為 0 的 NULL。此更改也以我不想要的格式給出 output。

不用為daily_phrase使用glueprint它更容易

make_phrase <- function(num, gift) {
  if (!is.character(gift[1])) {
    stop("Gift should be a string of characters") }
    if (!is.numeric(num[1])) {
    stop("Num should be a number") }
  num <- as.character(english(num))
  num <- ifelse(num == "one", "a", num)
  glue("{num} {gift}")
  }


sing_verse <- function(num, day, gift) {
  day <- day[num]
  daily_phrase <- make_phrase(num:1, gift[num:1]) 
  
  cat(paste("On the", day, "day of Christmas, my true love gave to me, \n"))
    print(daily_phrase)
}

現在,調用一個for循環

for(i in 1:3) sing_verse(i, xmas$day_in_words, xmas$gift_item)
On the first day of Christmas, my true love gave to me, 
a partridge in a pear tree
On the second day of Christmas, my true love gave to me, 
two turtle doves
a partridge in a pear tree
On the third day of Christmas, my true love gave to me, 
three french hens
two turtle doves
a partridge in a pear tree

數據

xmas <- structure(list(day = 1:6, day_in_words = c("first", "second", 
"third", "fourth", "fifth", "sixth"), gift_item = c("partridge in a pear tree", 
"turtle doves", "french hens", "calling birds", "golden rings", 
"geese a-laying")), row.names = c(NA, 6L), class = "data.frame")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM