X = 1:20
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
代表(X,2)
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
视图(REP(X,2))
使用R中的rep()函数生成20乘2向量时出现问题
而不是创建两个列,每个列从1到20运行,当我在R工作区中查看数据时,它显示为40X1向量,即1-20 1-20.
如何使用rep()函数创建20X2的重复列向量?谢谢.
我在R中有一个动物园对象的百分比变化.我想从1或100开始复合那些百分比变化,看看我们是否有复合扩展/衰减.
Lines <- "obs date pctchng comgrowth
1 2010-10-04 NA 100
2 2010-10-01 .15 NA
3 2010-09-30 .14 NA
4 2010-09-29 -.05 NA
5 2010-09-28 -.12 NA
6 2010-09-27 .07 NA
7 2010-09-24 -.15 NA
8 2010-09-23 .186 NA
9 2010-09-22 .01 NA
10 2010-09-21 .03 NA
11 2010-09-20 -.03 NA"
data <- read.zoo(textConnection(Lines), header=TRUE, index=2)
startobs <- 1
for (i in 1:100) {
data[startobs+i,"comgrowth"] <- data[startobs+i-1,"comgrowth"] *
(1+data[startobs+i,"pctchng"])
}
Run Code Online (Sandbox Code Playgroud)
我正在简化数据以简化显示,但这是我想要做的主意.我意识到问题是在加/减/除/乘时.出于某种原因我可以说data[startobs+i,"comgrowth"]=data[startobs+i-1,"comgrowth"]但是......
我不能说data[startobs+i,"comgrowth"]=data[startobs+i-1,"comgrowth"]+data[startobs+i-2,"comgrowth"].R不会让我添加+/ - /*等,并给我这个错误
Error in …Run Code Online (Sandbox Code Playgroud)