How to partition to multiple .csv from df based on whitespace row?

Viewed 26

I'm working with a database that has a timestamp, 3 numeric vectors, and a character vector.

Basically, each "set" of data is delineated by a new row. I need each series of rows to save as .csv when the row reads that each column is empty (x = \t\r\n). There's about 370 in my dataset.

For example,


library(dplyr)

data <- data.frame(x1 = 1:4,
                   x2 = 4:1,
                   x3 = 3,
                   x4 = c("text", "no text", "example", "hello"))

new_row <- c("\t\r\n", "\t\r\n", "\t\r\n", "\t\r\n")

data1 <- rbind(data, new_row)

data2 <- data.frame(x1 = 1:4,
                    x2 = 4:1,
                    x3 = 4,
                    x4 = c("text", "no text", "example", "hello"))

data2 <- rbind(data2, new_row)


data3 <- rbind(data1, data2)

view(data3)


This is what my data set looks like (without the timestamp). I need every set of consecutive rows after a row full or \t\r\n to be exported as an individual .csv.

I'm doing text analysis. Each group of rows, with highly variable group size, represents a thread on different subject. I need to analyze these individual threads.

What is the best way to go about doing this? I haven't had this problem before.

1 Answers
ind <- grepl("\t", data3$x4)
ind <- replace(cumsum(ind), ind, -1)
ind
#  [1]  0  0  0  0 -1  1  1  1  1 -1

data4 <- split(data3, ind)
data4
# $`-1`
#       x1    x2    x3    x4
# 5  \t\r\n \t\r\n \t\r\n \t\r\n
# 10 \t\r\n \t\r\n \t\r\n \t\r\n
# $`0`
#   x1 x2 x3      x4
# 1  1  4  3    text
# 2  2  3  3 no text
# 3  3  2  3 example
# 4  4  1  3   hello
# $`1`
#   x1 x2 x3      x4
# 6  1  4  4    text
# 7  2  3  4 no text
# 8  3  2  4 example
# 9  4  1  4   hello

The use of -1 was solely to keep the "\t\r\n" rows from being included in each of their respective groups, and we know that cumsum(ind) should start at 0. You can obviously drop the first frame :-)

From here, you can export with

data4 <- data4[-1]
ign <- Map(write.csv, data4, sprintf("file_%03d.csv", seq_along(data4)))
Related