R: vector or matrix? [duplicate] - r

This question already has answers here:
In R, how do you differentiate a result is vector or matrix?
(2 answers)
Closed 7 years ago.
Example code:
> sapply(list(1:3,25:29),median)
[1] 2 27
Is this output considered to be a vector or a matrix? Is there a command that I can use to determine this kind of information directly?

Hat-tip and thank you to thelatemail:
> is.vector(sapply(list(1:3,25:29),median))
[1] TRUE
> is.matrix(sapply(list(1:3,25:29),median))
[1] FALSE
As mentioned by him: The general pattern in R is as.xxx to convert and is.xxx to test.

Related

What is the R function and code for summing a list of characters in R? [duplicate]

This question already has answers here:
Count number of distinct values in a vector
(6 answers)
Closed 1 year ago.
I am stuck on a question in R, i need to sum a list of characters from the starwars dataset, which is the starwars eye color. The question asks how many different eye colours the character have. The answer is 15, which i derived from a table (table(starwars$eyecolor), but i cannot figure the code to get to 15.
Try any of below
> length(levels(factor(starwars$eye_color)))
[1] 15
> length(unique(starwars$eye_color))
[1] 15
> sum(!duplicated(starwars$eye_color))
[1] 15
length(unique(starwars$eye_color))

vector of historical highs of another vector [duplicate]

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)

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"

as.numeric changes the last digits of large integers [duplicate]

This question already has answers here:
Why are these numbers not equal?
(6 answers)
Closed 5 years ago.
When I to convert large numbers formatted as character strings to numeric R changes the last digits. This also happens when I pass it the number itself.
For example:
> options(scipen = 999)
> as.numeric("3411190080123000215")
[1] 3411190080123000320
> as.numeric(3411190080123000215)
[1] 3411190080123000320
This also happens when I use other numeric functions:
> floor(3411190080123000215)
[1] 3411190080123000320
Could this be an issue with my settings?
Thank you!!
The issue is that you are not actually using integers, you are using floats.
x <- as.numeric("3411190080123000215")
is.integer(x)
However, your number is too large to be stored as an integer anyway. Check out the gmp R package for arbitrarily large integers.

very simple subset selection in r [duplicate]

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%".

Resources