After scraping a pdf, I have a data frame with a chr text var:
df = data.frame(text = c("abc","def","abc","def"))
My question is how to turn it into:
df = data.frame(text1 = c("abc","abc"),text2=c("def","def"))
I am able to index the rows and manually rebuild a new df, but was curious if it could be done within the dplyr
pipe.
All solutions I have been able to find involve splitting each row, but not to split whole rows of a variable into new columns.
Using dplyr
you could create a new column (ind
) for grouping which would have same values every alternate rows and then we group_by
ind
and create a sequence column (id
) to spread
the data into two columns.
library(dplyr)
library(tidyr)
df %>%
mutate(ind = rep(c(1, 2),length.out = n())) %>%
group_by(ind) %>%
mutate(id = row_number()) %>%
spread(ind, text) %>%
select(-id)
# `1` `2`
# <fct> <fct>
#1 abc def
#2 abc def
A base R option would be to split
df
into separate dataframe every alternate rows creating a sequence using rep
and cbind
them together to form 2-column data frame.
do.call("cbind", split(df, rep(c(1, 2), length.out = nrow(df))))
# text text
#1 abc def
#3 abc def
We could do this in base R
. Use the matrix
route to rearrange a vector/column into a matrix
and then convert it to data.frame
(as.data.frame
). As the number of columns is constant i.e. 2, specify that value in ncol
as.data.frame(matrix(df$text, ncol = 2, byrow = TRUE,
dimnames = list(NULL, c('text1', 'text2'))))
# text1 text2
#1 abc def
#2 abc def
Or another option is unstack
from base R
after creating a sequence of alternate ids (making use of the recycling)
unstack(transform(df, val = paste0('text', 1:2)), text ~ val)
# text1 text2
#1 abc def
#2 abc def
Or we can split
into a list
of vector
s and then cbind
it together
as.data.frame(do.call(cbind, split(as.character(df$text), 1:2)))
# 1 2
#1 abc def
#2 abc def
Or another option is dcast
from data.table
library(data.table)
dcast(setDT(df), rowid(text)~ text)[, text := NULL][]
df <- data.frame(text = c("abc","def","abc","def"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With