【问题标题】:how to use "for loop'" in dataframe in r如何在 r 的数据框中使用“for loop”
【发布时间】:2020-04-24 15:28:46
【问题描述】:

我正在尝试使用 for 循环计算中国和美国位置的总价:

请参考以下示例数据框和代码:

z <- data.frame(location = c("china", "china", "US", "US" ), quantity = c(100, 200, 100, 200))

## Calculate Total price, considering price for one quanity is $1 for china and $3 for US

for row in 1:nrow(z) {

  l <- z[row, "location"]

  q <- z[row, "quanity"]

  ifelse (l == "china", 

          z$total <- (z$quantity * 1),

          z$total <- (z$quantity * 3)) 

【问题讨论】:

    标签: r loops


    【解决方案1】:

    在 R 中,大多数时候你可以不用循环。如果您只有 2 个位置,那么在这种情况下,您也可以使用 ifelse。试试

    transform(z, total = quantity * c(1, 3)[(location != "china") + 1])
    
    #  location quantity total
    #1    china      100   100
    #2    china      200   200
    #3       US      100   300
    #4       US      200   600
    

    如果您有多个这样的国家/地区,那么您也可以使用case_when from dplyr

    library(dplyr)
    z %>%
      mutate(total = case_when(location == "china"~quantity, 
                               location == "US"~quantity * 3, 
                               ....more countries))
    

    【讨论】:

    • 非常感谢它的工作!放在“函数”参数下是不是很好
    • @pralhad 如果这是您唯一要做的事情,则无需定义为函数。
    • 其实我想重复使用相同的功能。
    • @pralhad 是的,在这种情况下,您可以将它放在函数中以便可以重复使用。
    【解决方案2】:

    你根本不需要循环

    library(dplyr)
    
    z <- data.frame(location = c("china", "china", "US", "US" ),
                    quantity = c(100, 200, 100, 200))
    
    
    z %>% group_by(location) %>%
      summarise(sum_quantity = quantity %>% sum) %>% 
      mutate(total = if_else(location == 'china',
                                    sum_quantity,
                                    sum_quantity * 3))
    #> # A tibble: 2 x 3
    #>   location sum_quantity total
    #>   <fct>           <dbl> <dbl>
    #> 1 china             300   300
    #> 2 US                300   900
    
    
    # the ideal world
    
    new_z <- data.frame(location = c("china", "china", "US", "US" ),
                    quantity = c(100, 200, 100, 200),
                    value_rate = c(1,1,3,3))
    
    new_z %>% group_by(location) %>%
      summarise(sum_quantity = (quantity * value_rate) %>% sum)
    #> # A tibble: 2 x 2
    #>   location sum_quantity
    #>   <fct>           <dbl>
    #> 1 china             300
    #> 2 US                900
    

    reprex package (v0.3.0) 于 2020-01-07 创建

    【讨论】:

      【解决方案3】:

      我确实同意其他答案,即在这种特殊情况下,for 循环可能是一种矫枉过正,下面是我尝试为您提供一个 tidyverse 解决方案的尝试。但是,如果您确实想坚持循环范例,请查看 For each row in an R dataframe

      library(dplyr)
      #> 
      #> Attaching package: 'dplyr'
      #> The following objects are masked from 'package:stats':
      #> 
      #>     filter, lag
      #> The following objects are masked from 'package:base':
      #> 
      #>     intersect, setdiff, setequal, union
      library(magrittr)
      z <- data.frame(location = c("china", "china", "US", "US" ), quantity = c(100, 200, 100, 200))
      z %>% 
        mutate(price = ifelse(location == "china", 1, 3)) %>% 
        group_by(location) %>% 
        summarise(total = sum(quantity * price))
      #> # A tibble: 2 x 2
      #>   location total
      #>   <fct>    <dbl>
      #> 1 china      300
      #> 2 US         900
      

      reprex package (v0.3.0.9000) 于 2020-01-07 创建

      【讨论】:

        【解决方案4】:

        另一种基本 R 解决方案是使用 ifelse,如下所示:

        z <- within(z,total <- quantity*ifelse(location=="china",1,3))
        

        这样

        > z
          location quantity total
        1    china      100   100
        2    china      200   200
        3       US      100   300
        4       US      200   600
        

        【讨论】:

          猜你喜欢
          • 2021-12-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-11-17
          • 2020-05-31
          • 1970-01-01
          相关资源
          最近更新 更多