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.
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)
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
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"))
You could use
cSplit
. On your updated data frame,And on your original data frame,
Note: I'm using
splitstackshape
version 1.4.2. I believe thesep
argument has been changed from version 1.4.0You could use
extract
fromtidyr
to split theoriginalColumn
in totwo
columns. In the below code, I am creating 3 columns and removing one of the unwanted columns from the result.Using the updated
df
, (for better identification -df1
)Or without creating a new
column
indf
Based on the pattern in
df
, the below code also works. Here, theregex
used to extract the first column is(.)
. A dot is a single element at the beginning of the string inside the parentheses will be extracted for theCol1
. Then.{2}
two elements following the first are discarded and the rest within the parentheses(.*)
forms theCol2
.or using
strsplit
For
df1
, here is a solution usingstrsplit
You were close, here's a solution:
Notes:
stringr::str_split()
is better thanbase::strsplit()
because you don't have to doas.character()
, also it has then=2
argument you want to limit to only split on the first ':'