如何将数据框转换为时间序列(年度)。

huangapple go评论116阅读模式
英文:

how to convert a data frame to a time series (yearly)

问题

以下是要翻译的内容:

大家好。我有一个问题,需要将数据框转换为时间序列,但我的时间序列是基于年份的。所以基本上我想将表A转换为表B。表A,当前布局 我想要的

数据如下:
NameCorp<-c("AAC Holdings, Inc." ,"AAC Holdings, Inc." , "Accuride Corporation", "Accuride Corporation", "Accuride Corporation","Adeptus Health Inc." ,"Adeptus Health Inc." , "Akorn, Inc." ,"Akorn, Inc." ,"Akorn, Inc." ,"Aleris International, Inc.","Aleris International, Inc.","Aleris International, Inc.")

year<-c("2014","2015","2002","2005","2011","2018","2020","2012","2019","2020","2007","2009","2010")

value<-c("125","1115","300","500","2500","1100","47.5","150","250","900","2400","2025","500")

a<-data.frame(NameCorp=NameCorp,year=year,value=value)

我尝试了read.zoo()和ts(),但没有成功。

谢谢!!

英文:

everyone. i have the problem to convert a data frame to the time series, but my time series is based on year. so basically i want to convert the table A to table B.table A , current layout what i want

the data is here:
NameCorp<-c("AAC Holdings, Inc." ,"AAC Holdings, Inc." , "Accuride Corporation", "Accuride Corporation", "Accuride Corporation","Adeptus Health Inc." ,"Adeptus Health Inc." , "Akorn, Inc." ,"Akorn, Inc." ,"Akorn, Inc." ,"Aleris International, Inc.","Aleris International, Inc.","Aleris International, Inc.")

year<-c("2014","2015","2002","2005","2011","2018","2020","2012","2019","2020","2007","2009","2010")

value<-c("125","1115","300","500","2500","1100","47.5","150","250","900","2400","2025","500")

a<-data.frame(NameCorp=NameCorp,year=year,value=value)

i tried the read.zoo() and ts(), but it does not work.

Thank you !!!!

答案1

得分: 0

  1. 使用 tidyverse
  2. a %>%
  3. type_convert() %>%
  4. complete(NameCorp, year = full_seq(year, 1)) %>%
  5. pivot_wider(names_from = NameCorp, values_from = value)
  6. # 一个数据表: 19 × 6
  7. 年份 `AAC Holdings, Inc.` `Accuride Corporation` `Adeptus Health Inc.` `Akorn, Inc.` `Aleris International, Inc.`
  8. <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
  9. 1 2002 NA 300 NA NA NA
  10. 2 2003 NA NA NA NA NA
  11. 3 2004 NA NA NA NA NA
  12. 4 2005 NA 500 NA NA NA
  13. 5 2006 NA NA NA NA NA
  14. 6 2007 NA NA NA NA 2400
  15. 7 2008 NA NA NA NA NA
  16. 8 2009 NA NA NA NA 2025
  17. 9 2010 NA NA NA NA 500
  18. 10 2011 NA 2500 NA NA NA
  19. 11 2012 NA NA NA 150 NA
  20. 12 2013 NA NA NA NA NA
  21. 13 2014 125 NA NA NA NA
  22. 14 2015 1115 NA NA NA NA
  23. 15 2016 NA NA NA NA NA
  24. 16 2017 NA NA NA NA NA
  25. 17 2018 NA NA 1100 NA NA
  26. 18 2019 NA NA NA 250 NA
  27. 19 2020 NA NA 47.5 900 NA
英文:
  1. library(tidyverse)
  2. a %&gt;%
  3. type_convert() %&gt;%
  4. complete(NameCorp, year = full_seq(year, 1)) %&gt;%
  5. pivot_wider(names_from = NameCorp, values_from = value)
  6. # A tibble: 19 &#215; 6
  7. year `AAC Holdings, Inc.` `Accuride Corporation` `Adeptus Health Inc.` `Akorn, Inc.` `Aleris International, Inc.`
  8. &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
  9. 1 2002 NA 300 NA NA NA
  10. 2 2003 NA NA NA NA NA
  11. 3 2004 NA NA NA NA NA
  12. 4 2005 NA 500 NA NA NA
  13. 5 2006 NA NA NA NA NA
  14. 6 2007 NA NA NA NA 2400
  15. 7 2008 NA NA NA NA NA
  16. 8 2009 NA NA NA NA 2025
  17. 9 2010 NA NA NA NA 500
  18. 10 2011 NA 2500 NA NA NA
  19. 11 2012 NA NA NA 150 NA
  20. 12 2013 NA NA NA NA NA
  21. 13 2014 125 NA NA NA NA
  22. 14 2015 1115 NA NA NA NA
  23. 15 2016 NA NA NA NA NA
  24. 16 2017 NA NA NA NA NA
  25. 17 2018 NA NA 1100 NA NA
  26. 18 2019 NA NA NA 250 NA
  27. 19 2020 NA NA 47.5 900 NA

答案2

得分: 0

问题如下:

  • 数据是字符型的,尽管yearvalue应该表示数值。

    1. str(a)
    2. ## 'data.frame': 13 obs. of 3 variables:
    3. ## $ NameCorp: chr "AAC Holdings, Inc." "AAC Holdings, Inc."
    4. ## "Accuride Corporation" "Accuride Corporation" ...
    5. ## $ year : chr "2014" "2015" "2002" "2005" ...
    6. ## $ value : chr "125" "1115" "300" "500" ...
  • 问题没有显示不起作用的代码,所以可能还有其他错误。没有代码,我们无法解决这个问题。

  • 问题中显示的图像中的0应该是NA。

我们假设你希望得到一个zoo或ts对象,因为这是你尝试的函数可能会生成的对象,而且问题涉及时间序列。

以下代码将yearvalue中的字符值转换为数值,然后转换为zoo对象z,最后转换为ts对象tt。更多信息请参考?read.zoo

  1. library(zoo)
  2. z <- a %>%
  3. type.convert(as.is = TRUE) %>%
  4. read.zoo(FUN = c, index = "year", split = "NameCorp")
  5. tt <- as.ts(z)
  6. zz <- as.zoo(tt)

这是z的图表:

  1. library(ggplot2)
  2. autoplot(na.approx(z), facet = NULL)

如何将数据框转换为时间序列(年度)。

英文:

The problemd are:

  • the data is character even though year and value are supposed to represent numeric values.

    1. str(a)
    2. ## &#39;data.frame&#39;: 13 obs. of 3 variables:
    3. ## $ NameCorp: chr &quot;AAC Holdings, Inc.&quot; &quot;AAC Holdings, Inc.&quot;
    4. ## &quot;Accuride Corporation&quot; &quot;Accuride Corporation&quot; ...
    5. ## $ year : chr &quot;2014&quot; &quot;2015&quot; &quot;2002&quot; &quot;2005&quot; ...
    6. ## $ value : chr &quot;125&quot; &quot;1115&quot; &quot;300&quot; &quot;500&quot; ...
  • the question did not show the code that did not work so there could have been additional errors as well. We won't be able to address that without the code.

  • the 0's shown in the image in the question should likely be NA's

We assume you want a zoo or ts object since that is what the functions you tried would have produced and the subject refers to time series.

The following code converts the character values in year and value to numeric and then to a zoo object z and finally converts to ts object tt. See ?read.zoo for more information. We also convert tt back to zoo giving zz. 'z' has years actually present in the data whereas tt and zz have all years between the smallest and largest years.

  1. library(zoo)
  2. z &lt;- a |&gt;
  3. type.convert(as.is = TRUE) |&gt; # convert to numeric
  4. read.zoo(FUN = c, index = &quot;year&quot;, split = &quot;NameCorp&quot;)
  5. tt &lt;- as.ts(z)
  6. zz &lt;- as.zoo(tt)

Here is a graph of z

  1. library(ggplot2)
  2. autoplot(na.approx(z), facet = NULL)

如何将数据框转换为时间序列(年度)。

huangapple
  • 本文由 发表于 2023年6月8日 22:56:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76433156.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定