【问题标题】:R: If statement within for loop does not seem to be working?R:for 循环中的 if 语句似乎不起作用?
【发布时间】:2018-03-21 19:20:04
【问题描述】:

我有以下数据集:

我想查看 action_ID 列中的 ID 并检查它是否在 value 列中。如果是,我想查看关联变量是否为Comment。如果是Comment,我会将该ID 的cmets 数加1 到一个新的数据帧中,称为final,它由action_ID 和cmets 数组成。

这是我目前写的代码:

final = data.frame(action_ID = c(1001,981,734,985))

for (x in shares$action_ID) {
    if ((x %in% shares$value) & (shares$variable[shares$value == x] =="Comment")){
      final$num_comments[final$action_ID == x] =+ 1
    }else {
       final$num_comments[final$action_ID == x]  =+ 0
   }
}

每当我运行它都不起作用。我试图通过查看 if 语句中的第一个条件来调试它,结果由于某种原因 if 语句并没有真正起作用。 action_ID 中的每个值都会被输出。我也尝试使用任何不起作用的方法。

for (x in shares$action_ID){
    print(x)
    if (any(shares$value == x)){ # & (shares$variable[shares$value == x]== "Comment")){
        print(x)

    }
}

输出:

[1] "734"
[1] "1001"
[1] "1001"
[1] "985"
[1] "981"

感谢您的帮助!!

编辑: 我认为我对输出不是很清楚,我正在尝试创建一个表格来计算具有 0 cmets、1 条评论、2 cmets 等的帖子的数量。

【问题讨论】:

  • 最好使用dput 分享您的数据(或部分数据),而不是张贴图片
  • t <- table(df$action_ID[df$variable == 'Comment']); data.frame(t) 或者如果您需要所有 ID t <- table(factor(df$action_ID)[df$variable == 'Comment']); data.frame(t)

标签: r for-loop if-statement


【解决方案1】:

使用tidyverse 解决方案:

library(tidyverse)
df <- data_frame(action_ID = c(734, 1001, 985, 981),
                 variable = c("Photo", "Post", "Share", "Comment"),
                 value = c(234, 345, 1001, 1001))

# add the cnt variable before hand
df$cnt <- c(0)

df %>%
  filter(action_ID == value, variable == "Comment") %>%
  mutate(cnt = cnt + 1) %>%
  select(action_ID, cnt)

输出将是一个数据框,其中包含一个新列 cnt,其中包含所需的值。
你的例子的问题是你的标准没有得到满足。

【讨论】:

    【解决方案2】:

    你让循环变得比它需要的更难:

    因为听起来您想将 cmets 的计数按值链接回对应的 action_ID,所以先按 value 对 cme​​ts 进行计数,然后再加入主数据框是有意义的。

    require(dplyr)
    
    # create some data that mirrors yours
    
    set.seed(1112124)
    df <- data.frame(    
        action_ID= seq(1,10),
        variable = sample(c("Photo","Post","Share","Comment"),10,replace=T),
        value = sample(1:10,10,replace=T)
    )
    
    
    # first we tag each row with whether or not it's a comment (1 or 0)
    Comments <- df %>% mutate(Comment = ifelse(variable=="Comment",1,0)) %>% 
    
      # then group by value because we want to summarize at that level
      group_by(value) %>% 
      # then add up the number of comments for each ID
      summarize(N_Comments = sum(Comment))
    
      # now add our comment counts back to the original action IDs
    left_join(df, Comments, by = c("action_ID"="value"))
    
    
       action_ID variable value N_Comments
    1          1    Photo     5         NA
    2          2  Comment     7         NA
    3          3     Post     6         NA
    4          4  Comment     5         NA
    5          5    Share     9          1
    6          6  Comment     7          0
    7          7     Post    10          2
    8          8     Post    10          1
    9          9  Comment    10          0
    10        10  Comment     8          1
    

    【讨论】:

      猜你喜欢
      • 2016-01-08
      • 1970-01-01
      • 2011-07-23
      • 1970-01-01
      • 2017-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多