Home > Software engineering >  Separate values and value labels in vector
Separate values and value labels in vector

Time:07-26

I am trying to separate values (all numbers) and value labels (mix of numbers and characters) in separate vectors with the goal of ordering and labelling a factor variable.

Vector:

text <- "1 = Less than 1 month; 2 = 1 to 3 months; 3 = More than 4 months, less than 1 year"

Output:

numbers <- c("1", "2", "3")

characters <- c("Less than 1 month", "1 to 3 months", "More than 4 months, less than 1 year")

CodePudding user response:

read.table(text=gsub(";", '\n', text), sep='=', col.names = c('numbers', 'characters'))

  numbers                            characters
1       1                     Less than 1 month
2       2                         1 to 3 months
3       3  More than 4 months, less than 1 year

CodePudding user response:

Just for fun, here is an approach without read.table:

result <- data.frame(do.call(rbind, strsplit(trimws(unlist(strsplit(text, ";"))), " = ")))
colnames(result) <- c("numbers", "text")
result
#   numbers                                 text
# 1       1                    Less than 1 month
# 2       2                        1 to 3 months
# 3       3 More than 4 months, less than 1 year

CodePudding user response:

You can use stringr library

library(stringr)

x <- str_split(text , ";") |> lapply(\(x) str_split(x , " = "))

numbers <- trimws(sapply(x[[1]] , \(x) x[1]))

characters <- trimws(sapply(x[[1]] , \(x) x[2]))


  • output
> numbers
[1] "1" "2" "3"

> characters
[1] "Less than 1 month"                   
[2] "1 to 3 months"                       
[3] "More than 4 months, less than 1 year"

  • Related