循环,将月份列表作为R中的输入(loop with list of months as input in R)

编程入门 行业动态 更新时间:2024-10-27 10:19:55
循环,将月份列表作为R中的输入(loop with list of months as input in R)

我试图以数月为输入运行一个循环。 此外,我想要查看特定主题在特定日期出现的次数。 我试图这样做的方式如下?

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.

更多推荐

本文发布于:2023-08-04 22:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1423189.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:列表   loop   list   months   input

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!