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"))
Related
This question already has answers here:
which.max ties method in R
(4 answers)
Closed 2 years ago.
I have the problem, now you see that:
x<-c(1,2,3,6,4,5,6)
y=(which.max(x))
print(y)
*The result is 4 because it is the position of element 6 (max value). But I want the result returned is 4 and 7.
How can I do that?
Try the below:
which(x==max(x))
[1] 4 7
If you have potential NA values, use
which(x==max(x, na.rm=T))
This question already has answers here:
Calculating the maximum of sub-vectors of a larger vector
(2 answers)
Closed 3 years ago.
I searched at the forums but I could not find an answer for this.
I am looking for a way to convert
c(1,3,4,2,7,12,6,8,15)
to
c(1,3,4,4,7,12,12,12,15) using no loops.
It can be defined as a vector of historical highs of another vector.
This is an example, my data length will be fairly long.
thanks a lot,
Emre
We can use cummax from base R
cummax(v1)
#[1] 1 3 4 4 7 12 12 12 15
data
v1 <- c(1,3,4,2,7,12,6,8,15)
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
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
This question already has answers here:
How to convert a factor to integer\numeric without loss of information?
(12 answers)
Closed 10 years ago.
> X864291X8X74
[1] 8.0000000000 9.0000000000 10.0000000000 6.0000000000 8.0000000000
10 Levels: 0.0000000000 10.0000000000 12.0000000000 3.0000000000 4.0000000000 6.0000000000 ... NULL
> as.numeric(X864291X8X74)
[1] 8 9 2 6 8
what did I misunterstood? shouldn't be the result of as.numeric 8 9 10 6 8?
How do I get the correct result?
Your vector is a factor. This question has been asked quite a few times, ex: here, here, here. In order to convert a factor to numeric, you'll have to convert to character first. Try:
as.numeric(as.character(my_vec))
The documentation at ?factor states:
To transform a factor f to approximately its original numeric values,
as.numeric(levels(f))[f] is recommended and slightly more efficient
than as.numeric(as.character(f)).
So the following works as well:
as.numeric(levels(my_vec))[my_vec]