2016-08-04 173 views
0

的后续行一排的相互作用这是不准确的问题,但其总体思路创建R中的数据帧是与另一个数据帧

test<-data.frame(rbind(c(1,2,3), c(4, 5, 6), c(7, 8, 9), c(1, 2, 3))) 
names(test)[1] <- "one" 
names(test)[2] <- "two" 
names(test)[3] <- "three" 

所以我基本上要创建一个新的 - 含有一个列数据帧,其中每一行是行(I)行所述测试数据帧

如(I + 1):

test[1,1]-test[2,1] 
test[2,1]-test[3,1] 
test[3,1]-test[4,1]... 

I;已经试过:

k<-nrow(test) 
for(i in 1:k-1){ 
    test2<-data.frame(test[i,1]-test[i+1,1]) 
} 

但这只是生产一个值

也再添加一个最后一行“0”,所以有k行总

+0

PLE ase post expected result – Sotos

+1

这是你在找什么? 'diff(test $ one)' – rmf

+0

或者'data.frame(new_one = c(-diff(test $ one),0))' – Sotos

回答

0

为了您的行与行减法,您通过需要循环你的行和列:

k<-nrow(test) 
n <- ncol(test) 
for (j in 1:n){ 

    for(i in 1:k-1){ 

    test[i,n + j] <- test[i,j]-test[i+1,j] 
    i = i+1 
    } 
} 

要添加新行:

test[k+1,] <- matrix(0,nrow=1, ncol=n)