将字符串拆分为长数据帧格式的值 [英] Split Strings into values in long dataframe format

查看:46
本文介绍了将字符串拆分为长数据帧格式的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据帧,看起来像下面的示例 df ,它由字符变量 VAR 组成.

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

我想用回车符-换行符 \ r \ n 拆分字符变量,并在下面获取所需的数据帧:

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

我编写了如下代码,但是在尝试将数据帧的格式更改为长格式时,我在某种程度上迷失了 gather 函数.

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)

请告知.

推荐答案

我们可以使用 separate_rows

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

这篇关于将字符串拆分为长数据帧格式的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆