This question already has answers here:
Extract time from timestamp?
(4 answers)
Closed 4 years ago.
So I have a variable which measures the time that an entry was logged into the database, currently the form of these entries is:
"2018-09-22T14:00:09Z"
"2018-09-26T08:21:16Z"
Basically I need to extract from these strings only the time section of the entry. So the 8 characters between T and Z. By the way it is always letters T and Z that surround the time.
I would also like to store these times in a new variable called "times" and this be of a time class.
Is this at all possible in R??
Thanks in advance.
One base R option uses sub:
ts <- c("2018-09-22T14:00:09Z", "2018-09-26T08:21:16Z")
out <- sub("^.*T(.*)Z$", "\\1", ts)
[1] "14:00:09" "08:21:16"
Related
This question already has answers here:
Extract the first (or last) n characters of a string
(5 answers)
Closed 2 years ago.
Well, I got a variable that shows numbers like this one: 13015064000992062, and I need to extract the four first numbers and put it in another column. In this case, it will be 1301.
A friend told me that it was using stringr library, but he can't remember the code.
I am working with a data frame. So I need to get a new variable called "canton", that are the four first digits from 'Identificador'
So it looks like this:
We can use substr
permMan19$newcol <- substr(permMan19$identificador, 1, 4)
This question already has an answer here:
format year-month to POSIXct [duplicate]
(1 answer)
Closed 3 years ago.
I have two different dataframe with different date formats
date1<-c('2001-01-30', '2001-02-25')
data2 <- c('200101','200102')
I want to convert these dates in the same format so I can merge my two different dataframes by date.
The frequency is on a monthly basis.
Can someone help me with this task?
EDIT(Caution): This is not best practice and the use of standard parsers is recommended. However, different requirements may come up and therefore this answer simply shows how to get the expected format in the OP and in a comment from OP.
We could use:
paste0(substring(data2,nchar(data2)-1,nchar(data2)),"/",substring(data2,1,4))
#[1] "01/2001" "02/2001"
To get the reverse:
paste0(substring(data2,1,4),"-",substring(data2,nchar(data2)-1,nchar(data2)))
[1] "2001-01" "2001-02
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:
Getting the last n elements of a vector. Is there a better way than using the length() function?
(6 answers)
Closed 5 years ago.
suppose I have daily time series data under variable name "prices", but im only interested in the past 100 days. How would i extract the last 100 elements from this variable?
Something equivalent to python's prices[-100:] but for R?
If it's a vector:
tail(prices, 100)
This question already has answers here:
Understanding the order() function
(7 answers)
Closed 6 years ago.
I'm pretty new to r but picking it up gradually.
My question is, I want to make my date vector start from the earliest date rather from the newest. I have about 50 odd rows and want it in order of earliest first.
head(dates1)
[1] "2016-03-04" "2016-02-26" "2016-02-19" "2016-02-12" "2016-02-05" "2016-01-29"
I've tried order() but it gives back numeric values, I want to keep them as dates.
Thanks if you can help.
Try the following:
dates1 <- c("2016-03-04", "2016-02-26",
"2016-02-19", "2016-02-12",
"2016-02-05", "2016-01-29")
dates1 <- as.Date(dates1)
sort(dates1)
Order returns the indices, to get the same result you can do the following:
dates1[order(dates1)]