我有以下数据集:
我想查看action_ID
列中的ID,并检查它是否在value
列中。如果是,我想查看关联变量是否为Comment
。如果是Comment
,我会将该ID的评论数量加1到一个名为final
的新数据框中,该数据框由action_ID
和数量组成评价。
这是我到目前为止编写的代码:
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条评论,1条评论,2条评论等的帖子数量。
答案 0 :(得分:0)
使用app.use(function(req, res, next) {
res.locals.success_msg = req.flash('success_msg');
res.locals.error_msg = req.flash('error_msg');
res.locals.error = req.flash('error');
next();
})
解决方案:
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)
包含所需的值
您的示例的问题是您的标准未得到满足。
答案 1 :(得分:0)
你正在努力使它比循环更难:
因为听起来您想要按值将评论数量链接回相应的action_ID
,所以首先按value
计算评论,然后再加入主数据框是有意义的
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