在循环中沿列移动

时间:2019-01-08 17:46:40

标签: r for-loop

我正在尝试使用以下数据创建此函数:

df<-read.table(text="x    y
0    1000
0    1000
4    1000
2    1000
10   1000
5    1000",header=T)

目的是获得x和y列的累积差。目前,我得到x的值有错误:

  

1:在mrowdiff [i] <-df [i + 1,]-df [i,]中:     要替换的项目数不是替换长度的倍数

我认为这与不设置第二列有关。我试图使其正常运行,但不幸地失败了:

rowdiff<-function(df,na.rm=F){
  mrowdiff<-numeric(nrow(df))
  for(i in 1:nrow(df))

    {
   mrowdiff[i]<-df[i+1,]-df[i,]
   if(na.rm==T){
     mrowdiff<- mrowdiff[!is.na(mrowdiff)]
  }

  }
do.call(rbind,mrowdiff)
}

电流输出: rowdiff(df,na.rm = T)

    [,1]
[1,]    0
[2,]    4
[3,]   -2
[4,]    8
[5,]   -5

我希望第二列为0。

2 个答案:

答案 0 :(得分:1)

您可以进行一些更改。下面是完整的功能:

rowdiff<-function(df,na.rm=F){
> mrowdiff <- df # you want mrowdiff to have the same basic structure as df, so start with making it equal to df (there are more efficient ways to do this)
> for(i in 1:nrow(df))
+ {
+     mrowdiff[i, ]<-df[i+1, ]-df[i, ] # calculate differences for both rows at once
+     }
> mrowdiff<- na.omit(mrowdiff) # remove missing values
> mrowdiff # there's nothing to rbind, since you've been working with a dataframe all along
  }

rowdiff(df)
   x y
1  0 0
2  4 0
3 -2 0
4  8 0
5 -5 0

答案 1 :(得分:1)

这是一种避免使用函数的简单方法,尽管您提到过...

for (j in 1:ncol(df)) {
    df[,paste0("rowdiff",j)] <- NA
    for (i in 2:nrow(df)) {
        df[i,paste0("rowdiff",j)] <- df[i,j] - df[i-1,j]
    }   
}

输出:

> df
   x    y rowdiff1 rowdiff2
1  0 1000       NA       NA
2  0 1000        0        0
3  4 1000        4        0
4  2 1000       -2        0
5 10 1000        8        0
6  5 1000       -5        0