分割列定界符R [英] splitting a column delimiter R

查看:92
本文介绍了分割列定界符R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下数据框。我想将最后一列拆分为2。需要根据唯一的第一列进行拆分:其余列都无所谓。

I have a dataframe as below. I want to split the last column into 2. Splitting needs to be done based upon the only first : and rest of the columns dont matter.

在新数据框中,将是4列。第三栏为(a,b,d),第四栏为(1,2:3,3:4:4)

In the new dataframe, there will be 4 columns. 3 rd column will be (a,b,d) while 4th column will be (1,2:3,3:4:4)

有什么建议吗?我的代码的第四行不起作用:(。我可以使用全新的解决方案或更正第4行

any suggestions? 4th line of my code doesnt work :(. I am okay with completely new solution or corrections to the line 4

employee <- c('John Doe','Peter Gynn','Jolie Hope')
salary <- c(3, 2, 1)
df <- data.frame(employee, salary, originalColumn = c("a :1", "b :2:3", "d: 3:4:4"))

as.data.frame(do.call(rbind, strsplit(df,":")))

------------ -------- update1

--------------------update1

以下解决方案效果很好。但是我需要一个经过修改的解决方案,因为我刚刚意识到第3列中的某些单元格不会包含在这种情况下,我希望该单元格中的文本在拆分该列后仅出现在第一列中

Below solutions work well. But i need a modified solution as I just realized that some of the cells in column 3 wont have ":". In such case i want text in that cell to appear in only 1st column after splitting that column

employee <- c('John Doe','Peter Gynn','Jolie Hope')
salary <- c(3, 2, 1)
df <- data.frame(employee, salary, originalColumn = c("a :1", "b", "d: 3:4:4"))


推荐答案

您可以使用 cSplit 。在更新的数据框中,

You could use cSplit. On your updated data frame,

library(splitstackshape)
cSplit(df, "originalColumn", sep = ":{1}")
#      employee salary originalColumn_1 originalColumn_2
# 1:   John Doe      3                a                1
# 2: Peter Gynn      2                b               NA
# 3: Jolie Hope      1                d            3:4:4

在原始数据框中,

df1 <- data.frame(employee, salary,   
                  originalColumn = c("a :1", "b :2:3", "d: 3:4:4"))
cSplit(df1, "originalColumn", sep = ":{1}")
#      employee salary originalColumn_1 originalColumn_2
# 1:   John Doe      3                a                1
# 2: Peter Gynn      2                b              2:3
# 3: Jolie Hope      1                d            3:4:4

注意:我m使用 splitstackshape 版本1.4.2。我相信 sep 参数已从1.4.0版更改

Note: I'm using splitstackshape version 1.4.2. I believe the sep argument has been changed from version 1.4.0

这篇关于分割列定界符R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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