简体   繁体   English

用月份列表循环作为R中的输入

[英]loop with list of months as input in R

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. 但是,所有日期的输出都为0。 Any ideas why? 有什么想法吗?

Cheers, 干杯,

Try data$Date == sprintf("%s 2005", i) . 试试data$Date == sprintf("%s 2005", i) Your attempt searches for the literal string "i 2005". 您尝试搜索文字字符串“ i 2005”。

However, the table function was designed for this. 但是, table函数是为此设计的。 Use gsub to remove the year: 使用gsub删除年份:

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. PS:下一次,请提供一个可复制的示例,以支持开发和测试解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM