r - Create new rows in data frame based on multiple values of column -
i have adjusted question bit more specific
i have searched specific answer question without success.
first of have data frame consisting of 48 variables, looks likes this:
> df text screen_name ... 1 text @sam , @su , @jim addressed peter 2 text @eric addressed margret 3 text @sarah , @adam addressed john
now extracting strings equal ("@\s+") , store them in new column
df$addressees <- str_extract_all(df$text, "@\\s+")
this gets me:
... screen_name addressees ... 1 peter c("@sam", "@su", "@jim") 2 margret @eric 3 john c("@sarah", "@adam")
now want create new data frame 2 columns new rows each "addressee" created repeating respective value of column "screen_name":
> df screen_name addressees 1 peter sam 2 peter su 3 peter jim 4 margret eric 5 john sarah 6 john adam
i have tried solutions similar approaches, none of them seems work.
thank help!
ok, reproducible example:
# create df ego <- c("peter","margaret","john") friends <- list(c("sam","su","jim"),c("eric"),c("sarah","adam")) df <- data.frame(ego,friends= i(friends),stringsasfactors = f) # use repeat function repeat rows times <- sapply(df$friends,length) df <- df[rep(seq_len(nrow(df)), times),] # assign unlisted friends df$friends <- unlist(friends)
Comments
Post a Comment