Home > database >  A function to detect words vs numbers in a row & move them?
A function to detect words vs numbers in a row & move them?

Time:10-14

Was wondering if there's any function/package that I can use in R to detect & differentiate word and numbers in a column of df and move them to another column? Not trying to move just certain words, but all that are letters/words.

Something like:

Name Value Value2
John 3423423
Kelly 24241
Tim 30989
Siobhan Tuscon
Jim Arizona

to

Name Value Value2
John 3423423
Kelly 24241
Tim 30989
Siobhan Tuscon
Jim Arizona

Thanks!

CodePudding user response:

You may do this using regex -

library(dplyr)

df %>%
  mutate(condition = grepl('^\\d $', Value),
         Value1 = ifelse(condition, Value, ''), 
         Value2 = ifelse(!condition, Value, '')) %>%
  select(-condition, -Value)

#     Name  Value1  Value2
#1    John 3423423        
#2   Kelly   24241        
#3     Tim   30989        
#4 Siobhan          Tuscon
#5     Jim         Arizona

'^\\d $' returns TRUE for values which has all numbers in them.

data

It is easier to help if you provide data in a reproducible format

df <- structure(list(Name = c("John", "Kelly", "Tim", "Siobhan", "Jim"
), Value = c("3423423", "24241", "30989", "Tuscon", "Arizona"
)), row.names = c(NA, -5L), class = "data.frame")

CodePudding user response:

Here is one way using str_remove with regex:

library(dplyr)
library(stringr)
df %>% 
  mutate(Value1 = str_remove(Value,"[0-9].*"),
         Value=str_remove(Value,"^[A-Za-z] $"))
     Name   Value  Value1
1    John 3423423        
2   Kelly   24241        
3     Tim   30989        
4 Siobhan          Tuscon
5     Jim         Arizona
  •  Tags:  
  • r
  • Related