简体   繁体   English

将几个空行添加到 R 中的数据框中的最优雅方法?

[英]Most elegant ways to add a few empty rows into a data frame in R?

This is an example of what I want to do:这是我想要做的一个例子:

df <- data.frame(x=1:100,y=1:100)
empty_row_ids <- c(5,10)

This is the final output:这是最终的输出:

results <- rbind(data.frame(x=1:4,y=1:4),c(0,0),data.frame(x=5:8,y=5:8),c(0,0).data.frame(x=9:100,y=9:100)) 

Essentially, I want to insert some empty rows at some given numbers.本质上,我想在某些给定数字处插入一些空行。

A way to do what you want would be formating the empty_rows_id inside a dataframe with the zeroes and then use bind_rows() in a dplyr pipeline to add the data.一种方法做你想要的东西会在格式化的empty_rows_id与零一个数据帧里面,然后用bind_rows()dplyr管道添加数据。 Here the code:这里的代码:

library(dplyr)
#Data
df <- data.frame(x=1:100,y=1:100)
empty_row_ids <- c(5,10)
#Create data for rows
dfindex <- data.frame(id=empty_row_ids,x=0,y=0)
#Now bind
df2 <- df %>% mutate(id=1:n()) %>%
  bind_rows(dfindex) %>%
  arrange(id) %>% select(-id)

Output (some rows):输出(某些行):

      x   y
1     1   1
2     2   2
3     3   3
4     4   4
5     5   5
6     0   0
7     6   6
8     7   7
9     8   8
10    9   9
11   10  10
12    0   0
13   11  11
14   12  12
15   13  13

If you want to export to other source to format your tables, it would be better to use NA instead of zero as @MrFlick said.如果您想导出到其他来源以格式化您的表格,最好使用NA而不是 @MrFlick 所说的零。

I'd recommend looking for a formatting solution, rather than one that changes your data.我建议寻找格式化解决方案,而不是更改数据的解决方案。 But you can use add_row() to do what you're asking.但是您可以使用add_row()来完成您的要求。

library(dplyr)

n <- 2 # number of spacing rows to insert at each point
spacer <- rep("", n)

df %>%
  mutate(across(.fns = as.character)) %>%
  add_row(x = spacer, y = spacer, .before = 6) %>%
  add_row(x = spacer, y = spacer, .before = 11) 

Output:输出:

      x   y
1     1   1
2     2   2
3     3   3
4     4   4
5          
6          
7     5   5
8     6   6
9     7   7
10         
11         
12    8   8
13    9   9
14   10  10
...

Insert as many spacing rows n as you desire with rep("", n) at the target indices.使用rep("", n)在目标索引处插入尽可能多的间距行n

Here's a base R solution这是一个基本的 R 解决方案

add_empty_rows <- function(df, at) {
  new_len <- nrow(df) + length(at)
  result <- as.data.frame(lapply(df, function(x) vector(class(x), new_len)))
  result[(1:new_len)[-at],] <- df
  result
}
newdf <- add_empty_rows(df, empty_row_ids)

Basically we just make a new data.frame with all blank rows, then put the values we want to keep in the slots we don't want to keep empty基本上我们只是用所有空白行创建一个新的 data.frame,然后将我们想要保留的值放在我们不想保留为空的插槽中

Here is one way manipulating row index :这是操作行索引的一种方法:

inds <- seq(nrow(df))
df1 <- df[c(inds  - cumsum(inds %in% empty_row_ids), 
            tail(inds, length(empty_row_ids))), ]

row.names(df1) <- NULL
df1[empty_row_ids, ] <- 0
df1

#      x   y
#1     1   1
#2     2   2
#3     3   3
#4     4   4
#5     0   0
#6     5   5
#7     6   6
#8     7   7
#9     8   8
#10    0   0
#11    9   9
#12   10  10
#13   11  11
#..
#..
#99   97  97
#100  98  98
#101  99  99
#102 100 100

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM