R项目:绘制数据框

发布于 2024-10-29 19:30:34 字数 955 浏览 5 评论 0原文

9059 2011-02-18                 2.81
9060 2011-02-21                 <NA>
9061 2011-02-22                 2.72
9062 2011-02-23                 2.75
9063 2011-02-24                 2.73
9064 2011-02-25                  2.7
9065 2011-02-28                 2.73
9066 2011-03-01                 2.75
9067 2011-03-02                 2.77
9068 2011-03-03                 2.79
9069 2011-03-04                 2.81
9070 2011-03-07                 2.81
9071 2011-03-08                 2.83
9072 2011-03-09                 2.78
9073 2011-03-10                 2.72
9074 2011-03-11                 2.76
9075 2011-03-14                 2.75
9076 2011-03-15                  2.7

上面是名为“mydata”的数据框的片段。第一列的名称为 V1,第二列的名称为 V2。第一列包含使用 as.Date() 函数转换的日期。

我想将其绘制在 xy 网格上,其中 x 轴为第 1 列,y 轴为第 2 列。我已经尝试了几个小时的各种组合但没有成功。

我尝试过

> plot(mydata[,1], mydata[,2])

这会导致 R 控制台挂起,因为它似乎正在渲染一些无意义的内容。

9059 2011-02-18                 2.81
9060 2011-02-21                 <NA>
9061 2011-02-22                 2.72
9062 2011-02-23                 2.75
9063 2011-02-24                 2.73
9064 2011-02-25                  2.7
9065 2011-02-28                 2.73
9066 2011-03-01                 2.75
9067 2011-03-02                 2.77
9068 2011-03-03                 2.79
9069 2011-03-04                 2.81
9070 2011-03-07                 2.81
9071 2011-03-08                 2.83
9072 2011-03-09                 2.78
9073 2011-03-10                 2.72
9074 2011-03-11                 2.76
9075 2011-03-14                 2.75
9076 2011-03-15                  2.7

The above is a snippet of a data frame named 'mydata'. The name of the first column is V1 and the second V2. The first column contains dates which was converted using the as.Date() function.

I want to plot this on a x-y grid with column-1 on the x-axis and column-2 on the y-axis. I've tried various combinations for hours without success.

I tried

> plot(mydata[,1], mydata[,2])

This causes the R console to hang as it appears that it is rendering something nonsensical.

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

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

发布评论

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

评论(2

花开浅夏 2024-11-05 19:30:34

这对我有用(尽管我不知道它是否会产生所需的输出?

usr <- textConnection("9059 2011-02-18                 2.81
9060 2011-02-21                 <NA>
9061 2011-02-22                 2.72
9062 2011-02-23                 2.75
9063 2011-02-24                 2.73
9064 2011-02-25                  2.7
9065 2011-02-28                 2.73
9066 2011-03-01                 2.75
9067 2011-03-02                 2.77
9068 2011-03-03                 2.79
9069 2011-03-04                 2.81
9070 2011-03-07                 2.81
9071 2011-03-08                 2.83
9072 2011-03-09                 2.78
9073 2011-03-10                 2.72
9074 2011-03-11                 2.76
9075 2011-03-14                 2.75
9076 2011-03-15                  2.7")

usr <- read.table(usr, header = FALSE)[-1] #remove the first column

with(usr, plot(V3 ~ V2))

This works for me (although I don't know if it produces the desired output?

usr <- textConnection("9059 2011-02-18                 2.81
9060 2011-02-21                 <NA>
9061 2011-02-22                 2.72
9062 2011-02-23                 2.75
9063 2011-02-24                 2.73
9064 2011-02-25                  2.7
9065 2011-02-28                 2.73
9066 2011-03-01                 2.75
9067 2011-03-02                 2.77
9068 2011-03-03                 2.79
9069 2011-03-04                 2.81
9070 2011-03-07                 2.81
9071 2011-03-08                 2.83
9072 2011-03-09                 2.78
9073 2011-03-10                 2.72
9074 2011-03-11                 2.76
9075 2011-03-14                 2.75
9076 2011-03-15                  2.7")

usr <- read.table(usr, header = FALSE)[-1] #remove the first column

with(usr, plot(V3 ~ V2))
永不分离 2024-11-05 19:30:34

我让它工作执行以下操作:

> x2 <- read.zoo("2_yr_tsy_rate.txt", format="%m/%d/%Y", sep=",", na.strings="ND")
>
> x10 <- read.zoo("10_yr_tsy_rate.txt", format="%m/%d/%Y", sep=",", na.strings="ND")
>
> sprd <- x10 - x2
>
> plot(sprd)

这有效...但是如果我使用 read.csv 加载数据并尝试更改数据对象,那将是一场噩梦。

现在我有另一个问题。该图有效,但 x 轴标签每 10 年以年为单位报告一次。我的数据是自 1976 年以来的利差。我希望 x 轴标签为季度。我尝试了各种方法,包括以下内容,但出现了错误。

> plot(as.yearqtr(sprd), sprd, type='l')
Error in if (del == 0 && to == 0) return(to) : 
  missing value where TRUE/FALSE needed

这是 str(我的对象) 的输出

> str(sprd)
‘zoo’ series from 1976-06-01 to 2011-03-31
  Data: num [1:9088] 0.68 0.71 0.7 0.77 0.79 0.79 0.82 0.86 0.83 0.83 ...
  Index: Class 'Date'  num [1:9088] 2343 2344 2345 2346 2349 ...
> 

I got it to work doing the following:

> x2 <- read.zoo("2_yr_tsy_rate.txt", format="%m/%d/%Y", sep=",", na.strings="ND")
>
> x10 <- read.zoo("10_yr_tsy_rate.txt", format="%m/%d/%Y", sep=",", na.strings="ND")
>
> sprd <- x10 - x2
>
> plot(sprd)

this worked... but if I load the data using read.csv and try to change the data object it is a nightmare.

Now I have another problem. The plot works but the x-axis label reported in years every 10 years. My data is the interest rate spread going back to 1976. I would like the x-axis label to be quarterly. I tried various things including the following but got errors.

> plot(as.yearqtr(sprd), sprd, type='l')
Error in if (del == 0 && to == 0) return(to) : 
  missing value where TRUE/FALSE needed

This is the output of str(my object)

> str(sprd)
‘zoo’ series from 1976-06-01 to 2011-03-31
  Data: num [1:9088] 0.68 0.71 0.7 0.77 0.79 0.79 0.82 0.86 0.83 0.83 ...
  Index: Class 'Date'  num [1:9088] 2343 2344 2345 2346 2349 ...
> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文