Function "sample" in R does not work properly [duplicate] - r

This question already has answers here:
Sample from vector of varying length (including 1)
(4 answers)
Closed 1 year ago.
I do not understand how the sample function is working if I have a vector as argument with only one element to sample from (and I just want to sample one element).
I am doing a simulation and me input vector x is going to be of different size every time my loop runs one step. But when the vector is containing only one element, for example x = c(3), it should be sample 3, but I doesn't it starts to sample 2 and 1 all kind of numbers. I have tried to use set.seed and changed the replace argument, but something is wrong. Does anyone know what's going on here?

From ?sample:
If x has length 1, is numeric (in the sense of is.numeric) and x >= 1, sampling via sample takes place from 1:x. Note that this convenience feature may lead to undesired behaviour when x is of varying length in calls such as sample(x).
So this behavior is expected if undesirable. A possible solution is in the answer linked by user20650.

Related

r: How to sample from a population of size 1? [duplicate]

This question already has answers here:
Sample from vector of varying length (including 1)
(4 answers)
Closed 6 months ago.
I appreciate that sampling from a list of length 1 has little practical use yet all the same I tried the following:
When I run the r snippet sample(c(1,2,3),1) then I obtain a random single value from the list c(1,2,3).
When I run the r snippet sample(c(3),1) then I would expect the number 3 to always be output but I don't, I seem to obtain the same behaviour as above.
Why is this? How can I sample from a list of length 1?
I found that sample(c(3,3),1) does indeed output the intended, but feels not what I had in mind.
See documentation for sample:
If x has length 1, is numeric (in the sense of is.numeric) and x >= 1, sampling via sample takes place from 1:x.
You can use resample() from the gdata package. This saves you having to redefine resample in each new script. Just call
gdata::resample(c(3), 1)
https://www.rdocumentation.org/packages/gdata/versions/2.18.0/topics/resample

Is there a way to create this type of vector without using a for loop? [duplicate]

This question already has an answer here:
rep() with each equals a vector
(1 answer)
Closed 2 years ago.
I have a vector with a bunch of numbers in it, lets say 3,2,0,0,0,1,2,....
I want to make a vector that has numbers based on the numbers in the above vector.
It's hard to explain, but the vector created from the above numbers would be 1,1,1,2,2,6,7,7
One appears three times because the number in the first spot is a three, two shows up twice because the second number is a two, and so on.
I can do this just fine with a for loop using rep(), but I would love a way to do this with sapply and a custom function (or an already existing one if there is such a thing). I'm not sure how to do it without a counter variable i.
You can use rep in vectorized way here, looping over the position of each element with seq_along and repeating it x times.
x <- c(3,2,0,0,0,1,2)
rep(seq_along(x), x)
#[1] 1 1 1 2 2 6 7 7

flagging strings that appear in one vector but not another (R) [duplicate]

This question already has answers here:
Test if a vector contains a given element
(8 answers)
Closed 7 years ago.
I have a vector "A", and for each element of A I want to check whether it is equal to any element from a second vector "Targets". I want a vector of logical values with the length of A as return.
The same problem is mentioned here. Here is a related discussion of how to test if a vector contains a specific value, but I did not know how to apply this to my question because I want a vector as output and not a single boolean.
Here is example code:
#example data
A <- c(rep("A",4),rep(c("B","C"),8))
Targets <- c("A","C","D")
I would like a vector which tells me for each element of A if it is equal to at least one of the elements in Targets. For the example, this should produce a vector that is identical with:
result <- c(rep("TRUE",4), rep(c("FALSE","TRUE"), 8))
In case this is relevant, eventually vector A will be much longer (ca 20000 elements), and the vector Targets will contain approximately 30 elements.
Just try:
A %in% Targets
The %in% function tells you if each element of the first argument equals one of the elements of the second argument, that's exactly what you are looking for.

Sudden changes on the original digits of data frame in R, when tapply() applied [duplicate]

This question already has answers here:
Display exact value of a variable in R
(2 answers)
Closed 5 years ago.
I've used tapply() a lot in R, but I have no idea why the order of magnitudes are suddenly converted after tapply() function is applied.
When I load the original CSV data, the data shows as follows.
Barcode Group Price
1002-01-23 A 10.23568975
1002-01-24 A 2356.25
1002-01-25 A 123.54897
1002-01-26 A 200.1548794
However, after I use R codes, the digits of Price are converted as follows.
Barcode Group Price mean
1002-01-23 A 10.23569 672.5474
1002-01-24 A 2356.25000 672.5474
1002-01-25 A 123.54897 672.5474
1002-01-26 A 200.15488 672.5474
I would like to have 672.5473847875(=(10.23568975+2356.25+123.54897+200.1548794)/4) as a result of mean. How could I solve the problem? Let me show you my R codes.
barcode <- read.csv("barcode.csv",header=T)
barcode$Group <- as.factor(barcode$Group)
barcode$Price <- as.numeric(barcode$Price)
test <- tapply(barcode$Price, barcode$Group, mean)
test1 <- data.frame(Group=names(test), mean=test)
barcode$mean <- test1$mean[match(barcode$Group, test1$Group)]
I really need your help. Thank you so much.
The means are calculated correctly. The simplest way to see this is to test for it:
barcode$mean == 672.5473847875
[1] TRUE TRUE TRUE TRUE
You can change the default number of digits being printed by e.g.
options(digits=15)

R compare multiple values with vector and return vector [duplicate]

This question already has answers here:
Test if a vector contains a given element
(8 answers)
Closed 7 years ago.
I have a vector "A", and for each element of A I want to check whether it is equal to any element from a second vector "Targets". I want a vector of logical values with the length of A as return.
The same problem is mentioned here. Here is a related discussion of how to test if a vector contains a specific value, but I did not know how to apply this to my question because I want a vector as output and not a single boolean.
Here is example code:
#example data
A <- c(rep("A",4),rep(c("B","C"),8))
Targets <- c("A","C","D")
I would like a vector which tells me for each element of A if it is equal to at least one of the elements in Targets. For the example, this should produce a vector that is identical with:
result <- c(rep("TRUE",4), rep(c("FALSE","TRUE"), 8))
In case this is relevant, eventually vector A will be much longer (ca 20000 elements), and the vector Targets will contain approximately 30 elements.
Just try:
A %in% Targets
The %in% function tells you if each element of the first argument equals one of the elements of the second argument, that's exactly what you are looking for.

Resources