【问题标题】:R loop `sample()` function for each new row每个新行的R循环`sample()`函数
【发布时间】:2021-01-02 08:09:46
【问题描述】:
library(tidyverse)
fruit %>% 
  as_tibble() %>%
  transmute(fruit = value, fruit.abr = substring(value, 1, sample(3:6, 1)))

#> # A tibble: 80 x 2
#>    fruit        fruit.abr
#>    <chr>        <chr>    
#>  1 apple        app      
#>  2 apricot      apr      
#>  3 avocado      avo      
#>  4 banana       ban      
#>  5 bell pepper  bel      
#>  6 bilberry     bil      
#>  7 blackberry   bla      
#>  8 blackcurrant bla      
#>  9 blood orange blo      
#> 10 blueberry    blu      
#> # ... with 70 more rows

我希望我的水果缩写列是 3 到 6 个字符之间的随机字符串长度。每行将是不同的字符串长度(3 到 6 之间)。

我编写代码的方式是选择一次 3 到 6 之间的样本,然后将其用于每一行。如何“回收”或“循环”这个sample() 函数,使其为每一行选择一个新值(例如 3、6、4、3、5 等)?

【问题讨论】:

    标签: r loops sample


    【解决方案1】:

    sample(3:6, 1) 返回单个值,并将被回收到行的长度。您应该一次绘制与行数相同大小的样本。记得设置replace = TRUE进行替换采样。

    fruit %>% 
      as_tibble() %>%
      transmute(fruit = value, fruit.abr = substring(value, 1, sample(3:6, n(), TRUE)))
    
    # # A tibble: 10 x 2
    #    fruit        fruit.abr
    #    <chr>        <chr>    
    #  1 apple        "app"    
    #  2 apricot      "apr"    
    #  3 avocado      "avoca"  
    #  4 banana       "banana" 
    #  5 bell pepper  "bell "  
    #  6 bilberry     "bilbe"  
    #  7 blackberry   "blac"   
    #  8 blackcurrant "blac"   
    #  9 blood orange "blo"    
    # 10 blueberry    "blu"
    

    数据

    fruit <- structure(list(value = c("apple", "apricot", "avocado", "banana", 
    "bell pepper", "bilberry", "blackberry", "blackcurrant", "blood orange", 
    "blueberry")), class = "data.frame", row.names = c(NA, -10L))
    

    【讨论】:

    • 另一种选择是在转换之前添加rowwise()
    【解决方案2】:

    添加rowwise()

    fruit %>% 
         as_tibble() %>% 
         rowwise() %>% 
         transmute(fruit = value, fruit.abr = substring(value, 1, sample(3:6, 1)))
    
    # A tibble: 80 x 2
    # Rowwise: 
       fruit        fruit.abr
       <chr>        <chr>    
     1 apple        apple    
     2 apricot      apri     
     3 avocado      avocad   
     4 banana       bana     
     5 bell pepper  bell     
     6 bilberry     bil      
     7 blackberry   black    
     8 blackcurrant bla      
     9 blood orange blo      
    10 blueberry    blu      
    # ... with 70 more rows
    

    【讨论】:

    • 非常棒。 rowwise() 允许您在数据帧上一次计算一行。这在向量化函数不存在时最有用。
    【解决方案3】:

    试试这个,也许更接近你想要的。您可以使用 runif 创建一个介于 3 和 6 之间的随机索引,然后使用 sample() 将原始单词中的字符打乱。代码如下:

    #Data
    df <- data.frame(fruit=c('apple','orange'),stringsAsFactors = F)
    #My func
    myfunc<-function(x)
    {
      y <- unlist(strsplit(x,split=''))
      #Number
      index <- round(runif(1,3,6),0)
      #Create id
      var <- paste0(sample(y,index),collapse = '')
      #Return
      return(var)
    }
    #Apply
    df$ID <- apply(df,1,myfunc)
    

    输出:

       fruit   ID
    1  apple eppa
    2 orange egnr
    

    【讨论】:

      猜你喜欢
      • 2019-11-16
      • 2018-12-27
      • 1970-01-01
      • 1970-01-01
      • 2021-04-13
      • 1970-01-01
      • 2016-12-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多