2013-05-09 97 views
6

简要说明:如何将m行添加到我的m X n数据框中,其中每个新行在每个现有行后插入?我基本上会复制现有的行,但对一个变量进行更改。如何将行添加到每隔一行的R数据框?

更多详细信息:在参考another question时,我想我可以用rgl的segments3d函数来做我想要的。我有一组x,y,z点,但这些只是一组线段的一个端点。另一个终点在Z维上有很多米,作为第四个变量给出:X,Y,Z,Z_Length;在我的术语中,它是东向,北向,海拔,长度。

根据rgl文档,“点是成对的segment3d”。所以,我认为我需要修改我的数据框,以便每隔一行更改一次Z变量(从Z减去Z_Length)。在视觉上,它需要从这个去:

+-------+---------+----------+-----------+---------+ 
| Label | easting | northing | elevation | length | 
+-------+---------+----------+-----------+---------+ 
| 47063 | 554952 | 5804714 | 32.68  | 619.25 | 
| 47311 | 492126 | 5730703 | 10.40  | 1773.00 | 
+-------+---------+----------+-----------+---------+ 

这样:

+-------+---------+----------+-----------+---------+ 
| Label | easting | northing | elevation | length | 
+-------+---------+----------+-----------+---------+ 
| 47063 | 554952 | 5804714 | 32.68  | 619.25 | 
| 47063 | 554952 | 5804714 | -586.57 | 619.25 | 
| 47311 | 492126 | 5730703 | 10.40  | 1773.00 | 
| 47311 | 492126 | 5730703 | -1762.26 | 1773.00 | 
+-------+---------+----------+-----------+---------+ 

在链接的问题的数据样本是可用的。

回答

8

您的样本数据:

orig.df <- read.table(text = " 
Label easting northing elevation length 
47063 554952 5804714 32.68 619.25 
47311 492126 5730703 10.40 1773.00", header = TRUE) 

创建要插入的数据:

insert.df <- transform(orig.df, elevation = elevation - length) 

其附加到你的原始数据:

out.df <- rbind(orig.df, insert.df) 

重新排序行:

n <- nrow(orig.df) 
out.df[kronecker(1:n, c(0, n), "+"), ] 
# Label easting northing elevation length 
# 1 47063 554952 5804714  32.68 619.25 
# 3 47063 554952 5804714 -586.57 619.25 
# 2 47311 492126 5730703  10.40 1773.00 
# 4 47311 492126 5730703 -1762.60 1773.00 
+0

我想我需要在我的解决方案中使用某种排序,但克罗内克并不是我所期望的......你能解释它是如何做到的吗? – 2013-05-09 03:49:35

+2

看看'kronecker(1:5,c(0,5),“+”)'给你什么。对于第一个向量中的每个元素,它会在第二个向量中添加('FUN =“+”')所有元素,所以您会得到:1 + 0,1 + 5,2 + 0,2 + 5等。 '。当然还有其他方法可以获得这个指数向量,但我觉得这个有趣。 – flodel 2013-05-09 03:54:05

+2

这是狂野的,我喜欢它。你的大脑以奇妙的方式工作。 – 2013-05-09 04:12:07

2

这里是一个可能的方法,如果我知道你在做什么:

dat <- head(CO2, 10) # fake data set 

L1 <- lapply(1:nrow(dat), function(i) { 
    dat2x <- dat[i, ] 
    dat2x[4] <- dat[i, 4] - dat[i, 5] 
    rbind(dat[i, ], dat2x) 
}) 
do.call(rbind, L1) 

编辑:完全工作过e4e5f4出色的响应:

new <- dat[rep(1:nrow(dat),1,each=2),] 
new[c(F, T),4] <- dat[4] - dat[5] 

两者是等价的,但我认为改变更快。

+0

( +1)提供完整的答案。我没有注意到第4栏需要的改变! – Nishanth 2013-05-09 03:18:30

+1

是的,我第一次也错过了。 – 2013-05-09 03:19:26

0

您可以创建一个具有两倍行数的新矩阵,将旧数据框元素放回到新矩阵的适当位置,并留下间隙。在仰角上进行计算,创建一个新矩阵,然后将调整后的仰角矩阵插入到较大的新矩阵中。然后将矩阵转换回数据帧。

test <- matrix(1:9, ncol=3) 
ind <- (1:nrow(test)*2 - 1 # - 1 b/c you want to insert rows after, not before, existing rows 
test_new <- matrix(rep(NA, (nrow(test)*2*ncol(test))), ncol=ncol(test)) 
test_new[ind,] <- test 

test_elev <- test #create a new matrix that will have adjusted elevations 
test_elev[,2] <- test[,2] - test[,3] #e.g., test[,2] is the elevation column, and test[,3] is the length column 
test_new[ind+1,] <- test_elev #then put the new elevations into the new matrix 

#if you need it to be a data.frame() again, you can use `as.data.frame(test_new)` 
5

我不知道如何rgl进场这里,但如果你只是想创建一个新的data.frame有重复的值,请尝试:

df[rep(1:nrow(df),1,each=2),] 
+0

rgl,因为初稿问题更多的是针对细分领域。最终草案将其更改为更多的数据框查询。感谢你的回答。 – 2013-05-09 03:44:38

+0

代表1在代表(...,1,...)中做了什么?它没有它似乎工作正常? – Xizam 2017-01-30 14:41:21

2

从 “e4e5f4的” 响应

插入空行修改中间人行

# sample matrix of df 
    old <-matrix(1:9, ncol=3) 

    # double all rows 
    new <- old[rep(1:nrow(old),1,each=2),] 

    # replace all duplicates with blank cells 
    new[c(seq(2, dim(new)[1], by=2)), ] <- "" 

    old # original 
    new # all ok ;) 
相关问题