-7

my data have many columns with different names and want see all numeric values only in column name_id and store those values in z. I want z should contains only numeric values of column name_id of data, if any alphabet is there in column then it should not get store in z.

z <- unique(data$name_id)
z
#[1] 10 11 12 13 14 3  4  5  6  7  8  9 
#Levels: 10 11 12 13 14 3 4 5 6 7 8 9 a b c d e f

when i tried this

z <-  unique(as.numeric(data$name_id))
z
# [1]  1  2  3  4  5  6  7  8  9 10 11 12

output contains values only till 12 but column has values greater than 12 also

vidya nair
  • 61
  • 7

1 Answers1

0

Considering your data frame as

> b
 [1] "1"    "2"    "3"    "4"    "5"    "13"   "14"   "15"   "45"   "567"  "999"  "Name" "Age" 

Apply this :

regexp <- "[[:digit:]]+"
> z <- str_extract(b , regexp)
z[is.na(z)] <- ""

> z
 [1] "1"   "2"   "3"   "4"   "5"   "13"  "14"  "15"  "45"  "567" "999" ""    ""   

Hope this helps .

Pankaj Kaundal
  • 1,012
  • 3
  • 13
  • 25