What is the equivalent of .loc with multiple conditions in R?

huangapple go评论105阅读模式
英文:

What is the equivalent of .loc with multiple conditions in R?

问题

在R中,你可以使用以下方式来实现类似于Python中的.loc的多条件筛选:

  1. for (column in names(df)[2:9]) {
  2. for (i in 4:nrow(df)) {
  3. col <- paste0("AC", substring(column, nchar(column)))
  4. df[i, col] <- df$yw_lagged[df$id == df[i, column] & df$yearweek == df$yearweek[i]]
  5. }
  6. }

这段代码会在R中进行多条件筛选,并将结果赋值给指定的列,与你在Python中使用.loc相似。希望这有助于你在R中实现相同的功能。

英文:

I wonder if there is any equivalent of .loc in R where I am able to have multiple condition that would work in a for loop.

In python I accomplished this using .loc as seen in the code below. However, I am unable to reproduce this in R.

  1. for column in df.columns[1:9]:
  2. for i in range(4,len(df)):
  3. col = &#39;AC&#39; + str(column[-1])
  4. df[col][i] = df[&#39;yw_lagged&#39;].loc[(df[&#39;id&#39;] == df[column][i]) &amp; (df[&#39;yearweek&#39;] == df[&#39;yearweek&#39;][i])]

In R, I thought this would work

  1. df[i,col] &lt;- df[df$id == df[column][i] &amp; df$yearweek == df$yearweek[i], &quot;yw_lagged&quot;]

but it dont seem to filter in the same way as .loc does.

edit:

  1. structure(list(id = c(1, 2, 6, 7, 1, 2), v1 = c(2, 1, 1, 1, 2,
  2. 1), v2 = c(6, 3, 2, 2, 6, 3), v3 = c(7, 6, 5, 3, 7, 6), v4 =
  3. c(NA, 7, 7, 6, NA, 7), v5 = c(NA, 8, 14, 8, NA, 8), v6 = c(NA,
  4. NA,15, 15, NA, NA), v7 = c(NA, NA, 16, 16, NA, NA), v8 = c(NA,
  5. NA,NA, 17, NA, NA), violent = c(1, 0, 1, 0, 0, 0), yw_lagged =
  6. c(NA, NA, NA, NA, 1, 0), yearweek = c(20161, 20161, 20161, 20161,
  7. 20162, 20162), AC1 = c(NA, NA, NA, NA, NA, NA), AC2 = c(NA, NA,
  8. NA, NA, NA, NA), AC3 = c(NA, NA, NA, NA, NA, NA), AC4 = c(NA, NA,
  9. NA, NA, NA, NA), AC5 = c(NA, NA, NA, NA, NA, NA), AC6 = c(NA,
  10. NA, NA, NA, NA, NA), AC7 = c(NA, NA, NA, NA, NA, NA), AC8 = c(NA,
  11. NA, NA, NA, NA, NA)), row.names = c(NA, -6L), class = c(&quot;tbl_df&quot;,
  12. &quot;tbl&quot;, &quot;data.frame&quot;))

Picture of expected output (Tried to add some connections using colors)

答案1

得分: 0

以下是翻译好的部分:

  1. 样本数据缺少任何匹配项,因此我将覆盖 `yw_lagged` 的一些行:
  2. 从这里开始,
  3. ## dplyr
  4. ```r
  5. library(dplyr)
  6. df %>%
  7. group_by(yearweek) %>%
  8. mutate(across(matches("^v[0-9]+"),
  9. ~ yw_lagged[match(., id)],
  10. .names = "AC{sub('^v', '', .col)}")) %>%
  11. ungroup()
  12. # # A tibble: 6 × 20
  13. # id v1 v2 v3 v4 v5 v6 v7 v8 violent yw_lagged yearweek AC1 AC2 AC3 AC4 AC5 AC6 AC7 AC8
  14. # <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
  15. # 1 1 2 6 7 NA NA NA NA NA 1 1 20161 2 3 4 NA NA NA NA NA
  16. # 2 2 1 3 6 7 8 NA NA NA 0 2 20161 1 NA 3 4 NA NA NA NA
  17. # 3 6 1 2 5 7 14 15 16 NA 1 3 20161 1 2 NA 4 NA NA NA NA
  18. # 4 7 1 2 3 6 8 15 16 17 0 4 20161 1 2 NA 3 NA NA NA NA
  19. # 5 1 2 6 7 NA NA NA NA NA 0 1 20162 0 NA NA NA NA NA NA NA
  20. # 6 2 1 3 6 7 8 NA NA NA 0 0 20162 1 NA NA NA NA NA NA NA

data.table

  1. library(data.table)
  2. cols <- grep("v[0-9]+", names(df), value = TRUE)
  3. data.table(df)[, (sub("^v", "AC", cols)) :=
  4. lapply(.SD, \(z) yw_lagged[match(z, id)]),
  5. .SDcols = cols][]
  6. # id v1 v2 v3 v4 v5 v6 v7 v8 violent yw_lagged yearweek AC1 AC2 AC3 AC4 AC5 AC6 AC7 AC8
  7. # <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num>
  8. # 1: 1 2 6 7 NA NA NA NA NA 1 1 20161 2 3 4 NA NA NA NA NA
  9. # 2: 2 1 3 6 7 8 NA NA NA 0 2 20161 1 NA 3 4 NA NA NA NA
  10. # 3: 6 1 2 5 7 14 15 16 NA 1 3 20161 1 2 NA 4 NA NA NA NA
  11. # 4: 7 1 2 3 6 8 15 16 17 0 4 20161 1 2 NA 3 NA NA NA NA
  12. # 5: 1 2 6 7 NA NA NA NA NA 0 1 20162 2 3 4 NA NA NA NA NA
  13. # 6: 2 1 3 6 7 8 NA NA NA 0 0 20162 1 NA 3 4 NA NA NA NA

希望这些对您有所帮助。

英文:

The sample data is lacking any matches, so I'll override a few rows of yw_lagged:

  1. df$yw_lagged[1:4] &lt;- 1:4

From here,

dplyr

  1. library(dplyr)
  2. df %&gt;%
  3. group_by(yearweek) %&gt;%
  4. mutate(across(matches(&quot;^v[0-9]+&quot;),
  5. ~ yw_lagged[match(., id)],
  6. .names = &quot;AC{sub(&#39;^v&#39;, &#39;&#39;, .col)}&quot;)) %&gt;%
  7. ungroup()
  8. # # A tibble: 6 &#215; 20
  9. # id v1 v2 v3 v4 v5 v6 v7 v8 violent yw_lagged yearweek AC1 AC2 AC3 AC4 AC5 AC6 AC7 AC8
  10. # &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
  11. # 1 1 2 6 7 NA NA NA NA NA 1 1 20161 2 3 4 NA NA NA NA NA
  12. # 2 2 1 3 6 7 8 NA NA NA 0 2 20161 1 NA 3 4 NA NA NA NA
  13. # 3 6 1 2 5 7 14 15 16 NA 1 3 20161 1 2 NA 4 NA NA NA NA
  14. # 4 7 1 2 3 6 8 15 16 17 0 4 20161 1 2 NA 3 NA NA NA NA
  15. # 5 1 2 6 7 NA NA NA NA NA 0 1 20162 0 NA NA NA NA NA NA NA
  16. # 6 2 1 3 6 7 8 NA NA NA 0 0 20162 1 NA NA NA NA NA NA NA

data.table

  1. library(data.table)
  2. cols &lt;- grep(&quot;v[0-9]+&quot;, names(df), value = TRUE)
  3. data.table(df)[, (sub(&quot;^v&quot;, &quot;AC&quot;, cols)) :=
  4. lapply(.SD, \(z) yw_lagged[match(z, id)]),
  5. .SDcols = cols][]
  6. # id v1 v2 v3 v4 v5 v6 v7 v8 violent yw_lagged yearweek AC1 AC2 AC3 AC4 AC5 AC6 AC7 AC8
  7. # &lt;num&gt; &lt;num&gt; &lt;num&gt; &lt;num&gt; &lt;num&gt; &lt;num&gt; &lt;num&gt; &lt;num&gt; &lt;num&gt; &lt;num&gt; &lt;num&gt; &lt;num&gt; &lt;num&gt; &lt;num&gt; &lt;num&gt; &lt;num&gt; &lt;num&gt; &lt;num&gt; &lt;num&gt; &lt;num&gt;
  8. # 1: 1 2 6 7 NA NA NA NA NA 1 1 20161 2 3 4 NA NA NA NA NA
  9. # 2: 2 1 3 6 7 8 NA NA NA 0 2 20161 1 NA 3 4 NA NA NA NA
  10. # 3: 6 1 2 5 7 14 15 16 NA 1 3 20161 1 2 NA 4 NA NA NA NA
  11. # 4: 7 1 2 3 6 8 15 16 17 0 4 20161 1 2 NA 3 NA NA NA NA
  12. # 5: 1 2 6 7 NA NA NA NA NA 0 1 20162 2 3 4 NA NA NA NA NA
  13. # 6: 2 1 3 6 7 8 NA NA NA 0 0 20162 1 NA 3 4 NA NA NA NA

huangapple
  • 本文由 发表于 2023年1月8日 21:49:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75048255.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定