Splitting Text to Two Columns Using a Separator in R
I am trying to split the column using a separator "|"
but unfortunately the suggestions in Stack Overflow which recommended using separator()
is not working in my situation.
Can someone help me out here.
The data frame, I am using is given below:
structure(list(UniqueID = c("12M-MA | X1", "12M-MA | X2",
"12M-MA | X3", "12M-MA | X4", "12M-MA | X5"
), Cost = c(0.2, 0.3, 0.2, 0.2, 412.86), Actuals = c(0,
0, 0, 0, 32), Forecast = c(0, 0, 0, 0, 21), Value_Actuals = c(0,
0, 0, 0, 28341), Value_Forecast = c(0, 0, 0, 0, 652431
), `Forecast Accuracy` = c(0, 0, 0, 0, 8.51), `Max (Act vs Cons)` = c(0,
0, 0, 0, 652431)), row.names = c(NA, -5L), class = c("tbl_df",
"tbl", "data.frame"))
The code which I have used and the one which is working is this:
library(readxl)
library(dplyr)
library(writexl)
library(stringr)
Df<- read_excel("C:/X/X/X/X/YXZ-2023.xlsx"skip = 1)
Df <- Df %>%
separate(UniqueID,c("ABC","XYZ"),sep = "|")
The expectation is to get keep the column as is and get two more columns one with the name of "ABC" and the other with "XYZ".
12-MA should go into the column "ABC" and the other value such as X1,X2,X3,X4,X5 should go into column "XYZ".
Can someone help me out and let me now what is it that I am doing wrong. This was the suggestion in stack over flow and everywhere but isn't working for me.
Comments
Post a Comment