【问题标题】:Is there a way to pickout data based on associations using R?有没有办法使用 R 根据关联来挑选数据?
【发布时间】:2019-05-06 21:56:46
【问题描述】:

如果我有这样的数据集:

names <- c("Dave", "Ashley", "Drew")
score1 <- c(5, 1, 3)
opponent <- c("Drew", "Dave", "Ashley")
x <- cbind(names, score1, opponent)
x
y <- as.numeric(ifelse(x[, 3]==x[1, 1], x[1, "score1"], ifelse(
        x[, 3]==x[2, 1], x[2, "score1"], ifelse(
        x[, 3]==x[3, 1], x[3, "score1"], 1))))
y <- (y * score1)
x <- cbind(x, y)
x

我可以创建一个循环来创建一个新列,其中“score1”列中的数字乘以不同行中“y”列中的数字。例如,创建一个新列,其中 [1, 2] 处的值为 5,因为“Dave”在他的行中有“Drew”,所以“Dave”的“score1”列“5”乘以“Drew”' s “score1”栏“3”。有没有办法在一个适用于一百行和一百列的循环中做到这一点?目前,我知道的唯一方法是编写大量像上面这样的“ifelse”语句。

【问题讨论】:

    标签: r loops if-statement


    【解决方案1】:

    您可以执行自联接。不过,如果有重复的名字,你必须小心重复。:

    names <- c("Dave", "Ashley", "Drew")
    score1 <- c(5, 1, 3)
    opponent <- c("Drew", "Dave", "Ashley")
    
    x <- data.frame(names,score1,opponent)
    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
    new_df <- left_join(x,x[,-3],by=c("opponent"="names"))
    new_df
    #>    names score1.x opponent score1.y
    #> 1   Dave        5     Drew        3
    #> 2 Ashley        1     Dave        5
    #> 3   Drew        3   Ashley        1
    new_df <- mutate(new_df,y=score1.x * score1.y)
    new_df
    #>    names score1.x opponent score1.y  y
    #> 1   Dave        5     Drew        3 15
    #> 2 Ashley        1     Dave        5  5
    #> 3   Drew        3   Ashley        1  3
    

    reprex package (v0.2.1) 于 2019 年 5 月 7 日创建

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-18
      相关资源
      最近更新 更多