This question already has answers here:
How to sort a character vector where elements contain letters and numbers?
(6 answers)
Closed 3 years ago.
I have data that looks like the following, except the numbers are out of order:
dat<-
paste("Experience",1:20,sep="_")
Basically, I am trying to sort the columns in numerical order based on the ending number to order them as the code above produces. However, when I sort the values, it sorts based on the first digit as such:
"Experience_1" "Experience_10" "Experience_11" "Experience_12"
"Experience_13" "Experience_14" "Experience_15" "Experience_16"
"Experience_17" "Experience_18" "Experience_19" "Experience_2"
"Experience_20" "Experience_3" "Experience_4" "Experience_5"
"Experience_6" "Experience_7" "Experience_8" "Experience_9"
Thoughts?
The Stringr library, a part of the tidyverse, has str_sort() which sorts strings numerically in R.
library(stringr)
str_sort(dat, numeric = TRUE)
An option would be mixedsort from gtools
gtools::mixedsort(dat)
#[1] "Experience_1" "Experience_2" "Experience_3" "Experience_4" "Experience_5" "Experience_6"
#[7] "Experience_7" "Experience_8" "Experience_9" "Experience_10" "Experience_11" "Experience_12"
#[13] "Experience_13" "Experience_14" "Experience_15" "Experience_16" "Experience_17" "Experience_18"
#[19] "Experience_19" "Experience_20"
Related
This question already has answers here:
How to calculate the number of occurrence of a given character in each row of a column of strings?
(14 answers)
Count the number of pattern matches in a string
(6 answers)
Closed 2 years ago.
Say I have a strings
seq1 <- "ACTACTGGATGACT"
pattern1 <- "ACT"
What is the best way to find the number of times the pattern is in the sequence, in R? I would like to use a sliding window for loop, but im not clear on the proper way to handle the character strings.
We can use str_count
library(stringr)
str_count(seq1, pattern1)
#[1] 3
This question already has answers here:
Extracting numbers from vectors of strings
(12 answers)
Closed 3 years ago.
what is the most easiest way how to get number from string? I have huge list of links like this, I need to get that number 98548 from it.
https://address.com/admin/customers/98548/contacts
Note that number cant have different count of numbers and can start from 0 to 9
This is the most easiest that I know :
str <- "https://address.com/admin/customers/98548/contacts"
str_extract_all(str, "\\d+")[[1]]
Using stringr:
no="https://address.com/admin/customers/98548/contacts"
unlist(stringr::str_extract_all(no,"\\d{1,}"))
[1] "98548"
This question already has answers here:
Converting character to timestamp in dataframe
(2 answers)
Closed 4 years ago.
Some R date coercion fun.
The dates I'm trying to work with are organised as weird character strings. They have a "T" in the middle and a "Z" at the end, and include everything up to milliseconds, like this:
2018-01-01T00:00:00.000Z
I've tried to convert them into strings using the as.Date function:
first_interaction_date <- as.Date(first_last_reading_date$first_interaction, format = '%Y-%m-%dT%H:%M:%OSZ')
However, everything including and after the "T" gets cut off, like this:
2018-01-01.
I feel like there must be a really simple way to resolve this, but can't find it. I have both dplyr and stringr installed.
Many thanks!
use the package anytime:
library(anytime)
anytime(d1)
where d1 is your date vector
This question already has answers here:
Extract the last word between | |
(5 answers)
Closed 4 years ago.
I am practicing with regular expressions in R.
I would like to extract the last occurrence of two upper case letters.
I tried
>str_extract("kjhdjkaYY,","[:upper:][:upper:]")
[1] "YY"
And it works perfectly fine. What if I would like to extract the last occurrence of such pattern. Example:
function("kKKjhdjkaYY,")
[1] "YY"
Thank you for your help
We can use stri_extract_last_regex from stringi package
library(stringi)
stri_extract_last_regex("AAkjhdjkaYY,","[:upper:][:upper:]")
#[1] "YY"
Or if you want to stick with stringr, we can extract all the groups which match the pattern and then get the last one using tail
library(stringr)
tail(str_extract_all("AAkjhdjkaYY,","[:upper:][:upper:]")[[1]], 1)
#[1] "YY"
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to remove rows of a matrix by row name, rather than numerical index?
removing elements in one vector from another in R
I have two vectors:
a<-c(1,2,3,4,5,6,7,8)
b<-c(7,3,6,4,8,1)
I would like to select those elements of a which are not in b
I tried subset(a, a!=b) but I get the warning:
longer object length is not a multiple of shorter object length
Try setdiff for vectors:
R> setdiff(a,b)
[1] 2 5
Try this:
a[!(a%in%b)]
Look at ?"%in%".