user2658538
user2658538

Reputation: 367

Expand columns with vectors as items

I have a dataframe with the following format

Position   data     
48575185    1
48575150    3
48604751    5
48604673    c(3,5)
48591918    2
48591919    8

Some data in the data column is a numerical and some are vectors of numbers. I want to create a new dataframe that makes a new copy of the row with each value in the vector for rows with a vector item, essentially expand the table to:

Position   data     
48575185    1
48575150    3
48604751    5
48604673    3
48604673    5
48591918    2
48591919    8

How do I do this in r? I am very new to R. I found a function called expand(), but am unsure if that is what I need in this situation.

Upvotes: 1

Views: 1413

Answers (3)

Bryan
Bryan

Reputation: 51

I realize this answer is a bit late to the game, but here's an data.table implementation:

library(data.table)
dt = data.table(Position = c(48575185, 48575150, 48604751, 48604673, 48591918, 48591919),
                data = list(1,3,5,c(3,5), 2,8) )
dt[,lapply(.SD, unlist),.SDcols = 'data', by = 'Position']

Upvotes: 0

Philipp HB
Philipp HB

Reputation: 179

Try using unnest from the tidyr package. This allows you to convert each element in the list into a separate row. Assuming your data frame is called df

df %>% unnest(data)

Or if you want to unnest all columns

df %>% unnest()

Upvotes: 2

Maurits Evers
Maurits Evers

Reputation: 50678

We could use gsub with tidyr::separate_rows

df %>%
    mutate(data = gsub("(c\\(|\\))", "", data)) %>%
    separate_rows(data)
#  Position data
#1 48575185    1
#2 48575150    3
#3 48604751    5
#4 48604673    3
#5 48604673    5
#6 48591918    2
#7 48591919    8

Sample data

df <- read.table(text =
    "Position   data
48575185    1
48575150    3
48604751    5
48604673    c(3,5)
48591918    2
48591919    8", header = T)

Upvotes: 0

Related Questions