【问题标题】:How to add an empty row to a data frame and place it exactly between two specific existing rows?如何将空行添加到数据框中并将其准确放置在两个特定的现有行之间?
【发布时间】:2020-10-28 12:33:33
【问题描述】:

所以我有一个数据框,df1 有 992 行,另一个 df2 有 991 行(因为我必须从后者中删除其中一行)。我想将它们绑定在一起(使用 cbind),但它们有不同的行号,所以我想在 df1 的第 94 行和第 95 行之间添加一个空行。我该怎么做?

提前感谢您的任何回答。

【问题讨论】:

标签: r


【解决方案1】:

你可以这样做:

newdf <- rbind(df[1:94,], NA, df[95:nrow(df),])

【讨论】:

  • 需要重置行名
【解决方案2】:
> df <- data.frame(C1 = round(rnorm(100,10,1)),
+                  C2 = round(rnorm(100,10,1)))
> tail(df, 10)
    C1 C2
91   9 10
92  11 11
93  10 10
94   9 10
95  10 10
96  13 10
97  10  9
98   9  9
99   9 11
100 11  7
> df <- rbind(df[1:94, ], c('',''),df[95:100,])
> rownames(df) <- NULL
> tail(df, 10)
    C1 C2
92  11 11
93  10 10
94   9 10
95       
96  10 10
97  13 10
98  10  9
99   9  9
100  9 11
101 11  7
> 

【讨论】:

    猜你喜欢
    • 2022-01-25
    • 2020-08-11
    • 2020-12-09
    • 2018-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-30
    • 1970-01-01
    相关资源
    最近更新 更多