Hi Guys,
I am using 4.0.3. How do I solve this problem?
df1 <- c(Contact_NRIC.NO$CONTACT)
df1
[1] "11111111"
[2] "22222222/ 33333333/ 44444444"
[3] "55555555/ 66666666"
[4] "77777777"
[5] "88888888"
[6] "99999999/ 987654321/ 12345678/ 22334455"
[7] "44556677/ 88990011/ 11223344/55667788"
[8] "23456789 /98765432"
[9] "34567890/ 09876543"
[10] "09873467"
[11] "33445667"
[12] "88997766/ 12345678"
[13] "22334455/ 55443322"
df1_integer <- as.integer(df1)
Warning message:
NAs introduced by coercion
as.numeric(vec)
[1] 86482197 NA NA 93377359 97312158 NA NA NA
[9] NA 98456346 81805917 NA NA
Warning message:
NAs introduced by coercion
vec_new <- gsub(",","", vec)
vec_new
[[1] "11111111"
[2] "22222222/ 33333333/ 44444444"
[3] "55555555/ 66666666"
[4] "77777777"
[5] "88888888"
[6] "99999999/ 987654321/ 12345678/ 22334455"
[7] "44556677/ 88990011/ 11223344/55667788"
[8] "23456789 /98765432"
[9] "34567890/ 09876543"
[10] "09873467"
[11] "33445667"
[12] "88997766/ 12345678"
[13] "22334455/ 55443322"
as.numeric(vec_new)
[1] 11111111 NA NA 93377359 97312158 NA NA NA
[9] NA 98456346 81805917 NA NA
Warning message:
NAs introduced by coercion
suppressWarnings(as.numeric(vec))
[1] 86482197 NA NA 77777777 88888888 NA NA NA
[9] NA 09873467 33445667 NA NA
Hi, you could do something like this:
library(tidyverse)
df <- tibble(x = c("22222222/ 33333333/ 44444444", "1234"))
nmax <- max(stringr::str_count(df$x, "\\/")) + 1
df %>%
separate(x, paste0("col", seq_len(nmax)), sep = "\\/", fill = "right") %>%
mutate(across(everything(), parse_number))
# A tibble: 2 × 3
col1 col2 col3
<dbl> <dbl> <dbl>
1 22222222 33333333 44444444
2 1234 NA NA
Next time provide a reproducible example:
A minimal reproducible example consists of the following items:
A minimal dataset, necessary to reproduce the issue
The minimal runnable code necessary to reproduce the issue, which can be run
on the given dataset, and including the necessary information on the used packages.
Let's quickly go over each one of these with examples:
Minimal Dataset (Sample Data)
You need to provide a data frame that is small enough to be (reasonably) pasted on a post, but big enough to reproduce your issue.
Let's say, as an example, that you are working with the iris data frame
head(iris)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.5 1.4 0.…
FJCC
February 21, 2022, 3:49am
3
I am not sure what your goal is. Do you want a vector with 13 elements where everything is treated as a number? Would the 13th element be the calculated result of 22334455/55443322? Or do you want a numeric vector with 26 elements where the character / has been treated as a delimiter? I would do that like this:
library(stringr)
library(purrr)
OUT <- str_split(c("11111111",
"22222222/ 33333333/ 44444444",
"55555555/ 66666666",
"77777777",
"88888888",
"99999999/ 987654321/ 12345678/ 22334455",
"44556677/ 88990011/ 11223344/55667788",
"23456789 /98765432",
"34567890/ 09876543",
"09873467",
"33445667",
"88997766/ 12345678",
"22334455/ 55443322"),"/")
OUT_numeric <- map(OUT,.f = as.numeric)
OUT_numeric
[[1]]
[1] 11111111
[[2]]
[1] 22222222 33333333 44444444
[[3]]
[1] 55555555 66666666
[[4]]
[1] 77777777
[[5]]
[1] 88888888
[[6]]
[1] 99999999 987654321 12345678 22334455
[[7]]
[1] 44556677 88990011 11223344 55667788
[[8]]
[1] 23456789 98765432
[[9]]
[1] 34567890 9876543
[[10]]
[1] 9873467
[[11]]
[1] 33445667
[[12]]
[1] 88997766 12345678
[[13]]
[1] 22334455 55443322
FINAL <- unlist(OUT_numeric)
class(FINAL)
[1] "numeric"
FINAL
[1] 11111111 22222222 33333333 44444444 55555555 66666666
[7] 77777777 88888888 99999999 987654321 12345678 22334455
[13] 44556677 88990011 11223344 55667788 23456789 98765432
[19] 34567890 9876543 9873467 33445667 88997766 12345678
[25] 22334455 55443322
1 Like
Thank you, FJCC. I really appreciate this!
system
Closed
March 16, 2022, 11:35am
6
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.