need my string clean using regular expression in R programming [duplicate] - r

This question already has answers here:
Replace single backslash in R
(5 answers)
Closed 4 years ago.
My string is as below.
[{\"period\":\"01-06-2018\",\"count\":5},{\"period\":\"01-07-2018\",\"count\":8},{\"period\":\"01-08-2018\",\"count\":9}]
but I want only (only backslash) to be removed and it should look like below
(using R programming functions)
[{"period":"01-06-2018","count":5},{"period":"01-07-2018","count":8},{"period":"01-08-2018","count":9}]

This should do it:
library(tidyverse)
somestring<-c("[{\"period\":\"01-06-2018\",\"count\":5},{\"period\":\"01-07-2018\",\"count\":8},{\"period\":\"01-08-2018\",\"count\":9}]")
library(jsonlite)
fromJSON(somestring)
This yields:
period count
1 01-06-2018 5
2 01-07-2018 8
3 01-08-2018 9

Related

R - how to create sequence [duplicate]

This question already has answers here:
Create a vector that contains the first 10 powers of 2, then the first 10 powers of 3 by R language
(2 answers)
Closed 1 year ago.
How can I create this sequence in R?
I've been watching some guides but these are simple sequences.
It is vectorized. We can do
2^(1:10)/(1:10)

Need to extract only those digits which are having 5 to 6 digit length [duplicate]

This question already has answers here:
How to use grep()/gsub() to find exact match
(2 answers)
Closed 4 years ago.
I need to extract only 5 or 6 digit from a string. For example "hi 23456678 is number, also there is a number 92844 and 741653 "
I need to extract only the 5 or 6 digit number from string , i tried \d{5,6} but it is giving me result as (23456, 92844, 741653) but my desired outcome should be only 92844 & 741653 , how can i get that.
I am using R, please suggest.
You can try this
^[0-9]{5,6}$
{5,6} = between 5 and 6 characters

Convert string to numeric in R [duplicate]

This question already has answers here:
Evaluate expression given as a string
(8 answers)
Closed 5 years ago.
This seems likely to have a simple solution but I am having a hard time getting it. How do I convert this string to numeric?
> a <- "1:10"
Desired solution should be
> 1:10
[1] 1 2 3 4 5 6 7 8 9 10
I have tried as.numeric() (doesn't work), strsplit ":" and getting the end points 1 and 10 (can work but seems clumsy) but is there some simpler way? Thanks.
You can use eval() and parse()
eval(parse(text ="1:10"))

Need to convert a column or data to 0 or 1 in R [duplicate]

This question already has answers here:
Generate a dummy-variable
(17 answers)
Closed 6 years ago.
I am doing an assignment with the built-in "SWISS" dataset, and where the value is greater than 50 I need to create a binary variable taking 1
I have a feeling that I might have to use the subset command, but I am lost as how to implement it.
using dplyr
swiss %>%
mutate(aggGreaterThan50 = (Agriculture > 50) * 1)

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