在现有行之间添加空白行 [英] Add blank rows in between existing rows

查看:41
本文介绍了在现有行之间添加空白行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个240个案例的数据集,我想在每个现有行之后创建一个空白行。剩下480行,其中一半已填充,另一半是空的(然后我想自己填充一些数据)。

I have a dataset of 240 cases, in which I want to create a blank row after each existing row. Leaving me with 480 rows, of which half is filled and the other half is empty (which I then want to fill with some data myself).

数据示例

  id groep_MNC zkhs fbeh    pgebdat    p_age pgesl
1  3         1    1    1 1955-12-01 42.50000     1
2  5         1    1    1 1943-04-09 55.16667     1
3  7         1    1    1 1958-04-10 40.25000     1
4 10         1    1    1 1958-04-17 40.25000     1
5 12         1    1    2 1947-11-01 50.66667     1
6 14         1    1    2 1952-02-02 46.41667     1

理想情况下,应复制 id,如下所示:

Ideally, 'id' should be copied, thus looking like this:

    id groep_MNC zkhs fbeh    pgebdat    p_age pgesl
1    3         1    1    1 1955-12-01 42.50000     1
2    3        NA   NA   NA         NA       NA    NA
3    5         1    1    1 1943-04-09 55.16667     1
4    5        NA   NA   NA         NA       NA    NA
5    7         1    1    1 1958-04-10 40.25000     1
6    7        NA   NA   NA         NA       NA    NA
7   10         1    1    1 1958-04-17 40.25000     1
8   10        NA   NA   NA         NA       NA    NA
9   12         1    1    2 1947-11-01 50.66667     1
10  12        NA   NA   NA         NA       NA    NA
11  14         1    1    2 1952-02-02 46.41667     1
12  14        NA   NA   NA         NA       NA    NA

我尝试使用以下代码复制所有行:

I've tried copying all the rows with this code:

mydf_long <- mydf[rep(1:nrow(mydf), each = 2),]

但是您可以看到,甚至与我最终想要得到的结果都不相近。

But as you can see, that is not even close to what I want to end up with.

编辑
感谢您的修改和评论。我需要将原始数据转换为适合多级分析的格式。但是,数据仍然非常混乱,因此最初只对我的一小部分数据起作用的其他方法对我的整个数据集却无效。有关背景的更多信息,请参见我的其他问题:

Edit: Thanks for the edits and comments. I need to transform my original data to a format that is suitable for multilevel analyses. However, the data is still quite messy so other approaches that initially worked on a small subset of my data, didn't work on my full set. For more information about the background, see my other questions:

Reshape/gather function to create dataset ready for multilevel analysis

整理并重整凌乱的数据集(reshape / gather / unite函数)?

R-在if循环中给定的语句中,用同一df中的另一个值替换行中的值

由于我的合作伙伴变量相对较少,因此现在我想创建空白行,并用合作伙伴数据填充它们。

Since I have relative 'few' partner variables, I now want to create blank lines, and fill them in with the partner data.

推荐答案

我们可以复制每一行,然后将具有偶数行号的行设置为 NA

We can duplicate each row and then set the row with even row numbers to be NA.

dt2 <- dt[rep(1:nrow(dt), each = 2), ]
dt2[1:nrow(dt2) %% 2 == 0, ] <- NA

head(dt2)
    id groep_MNC zkhs fbeh    pgebdat    p_age pgesl
1    3         1    1    1 1955-12-01 42.50000     1
1.1 NA        NA   NA   NA       <NA>       NA    NA
2    5         1    1    1 1943-04-09 55.16667     1
2.1 NA        NA   NA   NA       <NA>       NA    NA
3    7         1    1    1 1958-04-10 40.25000     1
3.1 NA        NA   NA   NA       <NA>       NA    NA

DATA

dt <- read.table(text = "  id groep_MNC zkhs fbeh    pgebdat    p_age pgesl
1  3         1    1    1 1955-12-01 42.50000     1
2  5         1    1    1 1943-04-09 55.16667     1
3  7         1    1    1 1958-04-10 40.25000     1
4 10         1    1    1 1958-04-17 40.25000     1
5 12         1    1    2 1947-11-01 50.66667     1
6 14         1    1    2 1952-02-02 46.41667     1",
                 header = TRUE, stringsAsFactors = FALSE)

这篇关于在现有行之间添加空白行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆