Tom
Tom

Reputation: 2341

Subsetting data, if the column entry contains letters

I have data as follows:

DT <- as.data.frame(c("1","2", "3", "A", "B"))
names(DT)[1] <- "charnum"

What I want is quite simple, but I could not find an example on it on stackoverflow.

I want to split the dataset into two. DT1 with all the rows for which DT$charnum has numbers and DT2 with all the rows for which DT$charnum has letters. I tried something like:

DT1 <- DT[is.numeric(as.numeric(DT$charnum)),]

But that gives:

[1] 1 2 3 A B
Levels: 1 2 3 A B

Desired result:

> DT1
  charnum
1         1
2         2
3         3

> DT2
  charnum
1         A
2         B

Upvotes: 1

Views: 34

Answers (2)

akrun
akrun

Reputation: 887223

Using tidyverse

library(dplyr)
library(purrr)
library(stringr)
DT %>% 
   group_split(grp = str_detect(charnum, "\\d+"), .keep = FALSE) %>% 
   map(type.convert, as.is = TRUE)

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 389047

You can use regular expressions to separate the two types of data that you have and then separate the two datasets.

result <- split(DT, grepl('^\\d+$', DT$charnum))
DT1 <- type.convert(result[[1]])
DT1

#  charnum
#4       A
#5       B

DT2 <- type.convert(result[[2]])
DT2
#  charnum
#1       1
#2       2
#3       3

Upvotes: 2

Related Questions