Split Strings into values in long dataframe format [duplicate]

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-05 09:42:02

问题


I have a dataframe that looks like the following example df which consist of a character variable VAR.

df<-data.frame(ID = 1:2, 
               VAR = c("VAL1\r\nVAL2\r\nVAL8","VAL2\r\nVAL5"),
               stringsAsFactors = FALSE)
#     ID                  VAR
# 1    1 VAL1\r\nVAL2\r\nVAL8
# 2    2         VAL2\r\nVAL5

I would like to split the character variable by the return carriage - newline \r\n and obtain the desired dataframe below:

#    ID   VAR
# 1    1 VAL1
# 2    1 VAL2
# 3    1 VAL8
# 4    2 VAL2
# 5    2 VAL5

I wrote the code as follows, but I somehow got lost in the gather function while trying to change the format of the data frame into a long format.

library(tidyverse)
df <- df %>% 
  bind_cols(as.data.frame(str_split(df$VAR,"\r\n",simplify = TRUE))) %>%
  select(-VAR) %>%
  gather(key,value)

Please advise.


回答1:


We can do this with separate_rows

library(tidyr)
separate_rows(df, VAR, sep='\\s+')


来源:https://stackoverflow.com/questions/44922612/split-strings-into-values-in-long-dataframe-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!