Remove part of a string in R [duplicate] - r

This question already has answers here:
Changing date format in R
(7 answers)
Closed 2 years ago.
I'm new to R and I have tried the search function but couldn't make much sense of what I found.
I have a string like this: 20/08/2016
How can I convert it to 20/08/16
Any help is much appreciated.

Maybe you can try something like below
s <- "20/08/2016"
format(as.Date(s,"%d/%m/%Y"),"%d/%m/%y")
# [1] "20/08/16"
or
gsub("(.*/)\\d{2}(.*)","\\1\\2",s)
# [1] "20/08/16"

Related

R can not interpret big numbers correctly [duplicate]

This question already has answers here:
long/bigint/decimal equivalent datatype in R
(7 answers)
how to deal with BigINT in R [duplicate]
(1 answer)
Closed 2 years ago.
In R language, I have a problem regarding the big integers.
I type the number in the console, but the response is not equal to my input.
7241562255469626002
[1] 7241562255469626368
I also tried to set the digits option but it did not work:
options("digits" = 22)
7241562255469626002
[1] 7241562255469626368
What should I do to make it interpret the big numbers correctly?

R - substring by number [duplicate]

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"

How can I convert dates that have extra characters in the middle [duplicate]

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

Incorrect Date Format In R [duplicate]

This question already has answers here:
R Read abbreviated month form a date that is not in English
(2 answers)
Closed 4 years ago.
I try to change the format of "20FEB12:00:00:00" to Date and R gives me "NA"
DonĀ“t know what I am doing wrong:
strptime("20FEB12:00:00:00", "%d%b%y:%H:%M:%S")
Thanks!
Sys.setlocale(category = "LC_TIME",locale = "C")

Print r vector to copy paste into other code. [duplicate]

This question already has answers here:
How to print the structure of an R object to the console
(2 answers)
Closed 8 years ago.
I have a vector I'd like to copy paste into code to be able to produce a minimal working example.
Problem is, when I try to print the vector it produces output like
> head(residuals_list)
1 2 3 4 5 6
0.1833777 7.1833777 1.1833777 4.1833777 5.1833777 0.1833777
How do I get r to print c(0.1833777, 7.1833777, ...)?
With dput :
x =runif(5)
dput(x)
c(0.634340619435534, 0.833359521813691, 0.4804580679629, 0.119585362030193,
0.379494784167036)
Try str(residuals_list) to get a string version of the object

Resources