Reputation: 51
I have something similar to this set of data
891 Rehab 2a SQ002 1
892 Rehab 2a SQ002 1
893 Rehab 2a SQ002 1
894 Rehab 2a SQ002 2
895 Rehab 2a SQ002 2
896 Rehab 2a SQ002 1
897 Rehab 2a SQ002 2
898 Rehab 2a SQ002 2
899 Rehab 2a SQ002 1
900 Rehab 2a SQ002 1
and I am not sure how to separate the column (questionn) containing 2a into two separate columns one with "a" and one with "2". I have tried before with this
testra2<- testra1 %>%
separate(questionn,
into = c("num", "text"),
sep= "(?=[0-9])(?<=[A-Za-z])")
but it basically just creates a blank column and I get this warning message:
"Warning message: Expected 2 pieces. Missing pieces filled with
NA
in 2720 rows [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ...].
I would be grateful for any help, thank you very much:)
Upvotes: 1
Views: 2411
Reputation: 2210
testra2 <- testra1 %>%
separate(questionn,
into = c("num", "text"),
sep= "(?=[A-Za-z])")
Upvotes: 1
Reputation: 39174
You can separate by position.
library(tidyverse)
testra2<- testra1 %>%
separate(questionn,
into = c("num", "text"),
sep = 1)
Or use the extract
function.
testra3 <- testra1 %>%
extract(questionn, into = c("num", "text"), regex = "([0-9]+)([A-Za-z]+)")
DATA
testra1 <- read.table(text = "891 Rehab 2a SQ002 1
892 Rehab 2a SQ002 1
893 Rehab 2a SQ002 1
894 Rehab 2a SQ002 2
895 Rehab 2a SQ002 2
896 Rehab 2a SQ002 1
897 Rehab 2a SQ002 2
898 Rehab 2a SQ002 2
899 Rehab 2a SQ002 1
900 Rehab 2a SQ002 1 ",
header = FALSE, stringsAsFactors = FALSE)
names(testra1) <- c("V1", "V2", "questionn", "V3", "V4")
Upvotes: 5