在R

我已经从R加载了Arbuthnot集

data('arbuthnot')
arbuthnot<-data.frame(arbuthnot)

现在此数据集中的year变量是一个整数变量

str(arbuthnot)
'data.frame':   82 obs. of  3 variables:
 $ year : int  1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 ...
 $ boys : int  5218 4858 4422 4994 5158 5035 5106 4917 4703 5359 ...
 $ girls: int  4683 4457 4102 4590 4839 4820 4928 4605 4457 4952 ...

因此要将其转换为Date对象,请执行以下操作

arbuthnot$year<-strptime(arbuthnot$year,'%Y')
str(arbuthnot)
'data.frame':   82 obs. of  3 variables:
 $ year : POSIXlt,format: "1629-03-28" "1630-03-28" "1631-03-28" "1632-03-28" ...
 $ boys : int  5218 4858 4422 4994 5158 5035 5106 4917 4703 5359 ...
 $ girls: int  4683 4457 4102 4590 4839 4820 4928 4605 4457 4952 ...

我不希望R在年份变量中自动添加日期和月份。相反,我希望它仅包含年份变量。因此,理想情况下,我希望运行str(arbuthnot)看起来像这样。

str(arbuthnot)
'data.frame':   82 obs. of  3 variables:
 $ year : POSIXlt,format: "1629" "1630" "1631" "1632" ...
 $ boys : int  5218 4858 4422 4994 5158 5035 5106 4917 4703 5359 ...
 $ girls: int  4683 4457 4102 4590 4839 4820 4928 4605 4457 4952 ...

为什么R会自动添加当前月份和日期(今天是3月28日),我该怎么做才能停止呢?

hxhzz999 回答:在R

也许这样?

strftime("1629-03-28",format= '%Y',usetz = FALSE)
[1] "1629" 
本文链接:https://www.f2er.com/2563630.html

大家都在问