如何在每个其他行的R数据帧中添加行? [英] How can I add rows to an R data frame every other row?
问题描述
m
行添加到我的 m X n
数据框,其中每一行都是插入每个现有的行后?我将基本上复制现有行,但更改为一个变量。 更多细节:参考另一个问题,我想我可以用rgl的segment3d函数做我想要的。我有一组x,y,z点,但这些只是一组线段的一个终点。另一个终点在Z维度上距离很远,如第四个变量:X,Y,Z,Z_Length;在我的术语中,它的东西,东北,海拔,长度。
根据rgl文档,点数由segment3d成对进行。所以,我想我需要修改我的数据帧,每隔二行增加一个变量的Z变量(通过从Z中减去Z_Length)。从目前来看,需要从这里开始:
+ ------- + -------- - + ---------- + ----------- + --------- +
|标签|东部| northing |高程|长度|
+ ------- + --------- + ---------- + ----------- + ---- ----- +
| 47063 | 554952 | 5804714 | 32.68 | 619.25 |
| 47311 | 492126 | 5730703 | 10.40 | 1773.00 |
+ ------- + --------- + ---------- + ----------- + ---- ----- +
到:
+ ------- + --------- + ---------- + -------- --- + --------- +
|标签|东| | northing |高程|长度|
+ ------- + --------- + ---------- + ----------- + ---- ----- +
| 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 |
+ ------- + --------- + ---------- + ----------- + ---- ----- +
链接问题中的数据样本可用。
您的样本数据:
orig。 df< - read.table(text =
标签向东北纬度
47063 554952 5804714 32.68 619.25
47311 492126 5730703 10.40 1773.00,标题=真)
创建要插入的数据:
insert.df< - transform(orig.df,elevation = elevation - length)
将其附加到您的原始数据:
out.df< - rbind(orig.df,insert.df)
重新排列行:
code> n< - nrow(orig.df)
out.df [kronecker(1:n,c(0,n),+),]
#海拔长度
#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
Brief: how can I add m
rows to my m X n
data frame, where each new row is inserted after each existing row? I will essentially copy the existing row, but make a change to one variable.
More detail: in reference to another question, I think I can do what I want with rgl's segments3d function. I have a set of x,y,z points, but these are just one end point of a set of line segments. The other end point is so many metres away in the Z dimension, given as a fourth variable: X,Y,Z,Z_Length; in my terminology it's easting,northing,elevation,length.
According to the rgl docs, "Points are taken in pairs by segments3d". So, I think I need to modify my data frame to have extra entries every second line with an altered Z variable (by subtracting Z_Length from Z). Visually, it needs to go from this:
+-------+---------+----------+-----------+---------+
| Label | easting | northing | elevation | length |
+-------+---------+----------+-----------+---------+
| 47063 | 554952 | 5804714 | 32.68 | 619.25 |
| 47311 | 492126 | 5730703 | 10.40 | 1773.00 |
+-------+---------+----------+-----------+---------+
to this:
+-------+---------+----------+-----------+---------+
| 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 |
+-------+---------+----------+-----------+---------+
A data sample at the linked question is available.
Your sample data:
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)
Create your data to be inserted:
insert.df <- transform(orig.df, elevation = elevation - length)
Append it to your original data:
out.df <- rbind(orig.df, insert.df)
Reorder the rows:
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
这篇关于如何在每个其他行的R数据帧中添加行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!