List reformating in R -
this question has answer here:
i have df:
keggnumber cor colors x1 c00095 -2.623973e-01 red x2 c17714, c00044 -2.241113e-01 red x3 c00033 -3.066684e-01 red
and format 2 column dataframe each individual keggnumber
matched color
. this:
keggnumber colors c00095 red c17714 red c00044 red c00033 red
essentially, new dataframe take rows of old dataframe more 1 keggnumber
, splits them up, while keeping same color
each.
tidyr
makes quite easy:
library(tidyr) df %>% separate_rows(keggnumber) ## cor colors keggnumber ## 1 -0.2623973 red c00095 ## 2 -0.2241113 red c17714 ## 3 -0.2241113 red c00044 ## 4 -0.3066684 red c00033
chop off cor
column if like.
a less-pretty base option:
do.call(rbind, map(function(x, y){data.frame(keggnumber = x, colors = y)}, strsplit(as.character(df$keggnumber), ', '), df$colors)) ## keggnumber colors ## 1 c00095 red ## 2 c17714 red ## 3 c00044 red ## 4 c00033 red
Comments
Post a Comment