我试图以数月为输入运行一个循环。 此外,我想要查看特定主题在特定日期出现的次数。 我试图这样做的方式如下?
for (i in c("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")){ print(length(which(data$Date == "i 2005" & data$Maxtopic == 3))) }尽管如此,我得到0作为所有日期的输出。 有什么想法吗?
干杯,
I am trying to run a loop with months as the input. Moreover, I want to spot the number of times a given topic appears in a specific date. The way I am trying to do so is as follows?
for (i in c("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")){ print(length(which(data$Date == "i 2005" & data$Maxtopic == 3))) }Nevertheless, I get 0 as output for all the dates. Any ideas why?
Cheers,
最满意答案
试试data$Date == sprintf("%s 2005", i) 。 您尝试搜索文字字符串“i 2005”。
但是, table函数是为此而设计的。 使用gsub删除年份:
table(gsub(" 2005", "", data[data$Maxtopic == 3, "Date"], fixed = TRUE))PS:下次请提供可重复的示例,以便开发和测试解决方案。
Try data$Date == sprintf("%s 2005", i). Your attempt searches for the literal string "i 2005".
However, the table function was designed for this. Use gsub to remove the year:
table(gsub(" 2005", "", data[data$Maxtopic == 3, "Date"], fixed = TRUE))PS: Next time please provide a reproducible example to enable development and testing of solutions.
更多推荐
发布评论