问题
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