r {:条件有长度> 1

发布于 2025-02-10 09:24:27 字数 798 浏览 3 评论 0原文

这是我第一次在Stackoverflow中问一个问题,也是我第一次使用R进行编码 因此,请了解我的解释是否不清楚:(

我现在有一个数据框(Data2000),即1092 x 6 标题是年,月份,预测范围,公司名称,GDP价格指数和消费者价格指数

< img src =“ https://i.sstatic.net/gooqg.png” alt =“在此处输入图像说明”>

我想在GDPPI和CPI上创建每个月的矢量,

我的最终目标是获得卑鄙,中位数,四分位数范围和每个月的第90个百分位数范围,我认为这是第一步

,这是我写的代码,

***library(tidyverse)
data2000 <- read.csv("")
for (i in 1:12) {
  i_gdppi <- c()
  i_cpi <- c()
}
for (i in 1:12) {
  if (data2000$month == i) {
  append(i_gdppi,data2000[,gdppi])
  append(i_cpi, data2000[,cpi])
}
}***

不幸的是,我收到了一条错误消息,说 if(data2000 $ month == 1){:条件的错误; 1

我自己搜索了它,在if语句中,我不能将矢量用作条件 我该如何解决这个问题? 非常感谢您,祝您有美好的一天!

this is my first time asking a question in StackOverflow and also my first time coding using R
So, please understand if my explanation is unclear :(

I now have a data frame (data2000) that is 1092 x 6
The headers are year, month, predictive horizon, name of the company, GDP Price Index, and Consumer Price Index

enter image description here

I want to create vectors on gdppi and cpi for each month

My ultimate goal is to get the mean, median, interquartile range, and 90th-10th percentile range for each month and I thought this is the first step

and this is the code that I wrote by far

***library(tidyverse)
data2000 <- read.csv("")
for (i in 1:12) {
  i_gdppi <- c()
  i_cpi <- c()
}
for (i in 1:12) {
  if (data2000$month == i) {
  append(i_gdppi,data2000[,gdppi])
  append(i_cpi, data2000[,cpi])
}
}***

Unfortunately, I got an error message saying that
Error in if (data2000$month == 1) { : the condition has length > 1

I googled it by myself and in if statement, I cannot use a vector as a condition
How can I solve this problem?
Thank you so much and have a nice day!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我ぃ本無心為│何有愛 2025-02-17 09:24:27

如果您使用group_by()函数,则它需要按下子设置:

library(dplyr)

data2000 <- data.frame(month = rep(c(1:12), times = 2), gdppi = runif(24)*100) # Dummy data

data2000 |>
  group_by(month) |> 
  summarise(mean = mean(gdppi), q10 = quantile(gdppi, probs = .10), q25 = quantile(gdppi, probs = .25)) # Add the other percentiles, as needed

则给予此信息,然后group_by(年,月份)

# A tibble: 12 x 4
   month  mean   q10   q25
   <int> <dbl> <dbl> <dbl>
 1     1  12.5  3.44  6.83
 2     2  34.7  7.15 17.5 
 3     3  37.8 22.1  28.0 
 4     4  30.3 19.0  23.2 
 5     5  65.7 62.2  63.5 
 6     6  60.7 38.7  47.0 
 7     7  43.0 38.2  40.0 
 8     8  77.9 60.7  67.1 
 9     9  56.3 44.0  48.6 
10    10  53.1 19.6  32.2 
11    11  63.8 40.6  49.3 
12    12  59.0 49.2  52.9 

如果您有几年和几个月,

If you use the group_by() function then it takes care of sub-setting your data:

library(dplyr)

data2000 <- data.frame(month = rep(c(1:12), times = 2), gdppi = runif(24)*100) # Dummy data

data2000 |>
  group_by(month) |> 
  summarise(mean = mean(gdppi), q10 = quantile(gdppi, probs = .10), q25 = quantile(gdppi, probs = .25)) # Add the other percentiles, as needed

Gives this

# A tibble: 12 x 4
   month  mean   q10   q25
   <int> <dbl> <dbl> <dbl>
 1     1  12.5  3.44  6.83
 2     2  34.7  7.15 17.5 
 3     3  37.8 22.1  28.0 
 4     4  30.3 19.0  23.2 
 5     5  65.7 62.2  63.5 
 6     6  60.7 38.7  47.0 
 7     7  43.0 38.2  40.0 
 8     8  77.9 60.7  67.1 
 9     9  56.3 44.0  48.6 
10    10  53.1 19.6  32.2 
11    11  63.8 40.6  49.3 
12    12  59.0 49.2  52.9 

If you have years and months, then group_by(year, month)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文