Why does sample function gives me an error - r

I am doing a simple sampling using the following code in R.
x=1:1000
sample1=sample(x, size=30,replace = F)
It used to work before but it is not working. I get this error message:
Error in sample.int(length(x), size, replace, prob) : invalid 'replace' argument

Do not use F and T as boolean values in R! Always use full names FALSE and TRUE. The variables F and T can be re-assigned any value.
Restart R (if it displays [Previously saved workspace restored], also run rm(list=ls())) and try the following:
1 == 1
is.logical(c(TRUE, FALSE))
is.logical(c(T, F))
v <- 1:5
v <= 3
(v <= 3) == T
# [1] TRUE TRUE TRUE FALSE FALSE
F <- 'a'
T <- FALSE
v <- 1:5
v <= 3
(v <= 3) == T
# [1] FALSE FALSE FALSE TRUE TRUE
I've included the important output.

Related

How does the all function work in R using two expressions?

I have an issue with the all function in R.
let a and b two vectors:
a <- c(Inf,0)
b <- c(1,0)
When I try to evaluate the expression all(a==b) the function returns FALSE, is OK, if it is evaluated the expression all(a==Inf) the function returns FALSE, so far all is working OK, but if I try to evaluate the expression all((a==b) | (a==Inf)) the function returns TRUE.
Could someone explain me why?
The OR is done column wise:
a <- c(Inf,0)
b <- c(1,0)
(a==b)
#> [1] FALSE TRUE
(a==Inf)
#> [1] TRUE FALSE
(a==Inf)|(a==b)
#> [1] TRUE TRUE
In each column there's a TRUE so each column is TRUE
When you type help("|"), you will see that | is element-wise OR.
In this case, given
> (a == b)
[1] FALSE TRUE
> (a == Inf)
[1] TRUE FALSE
the expression (a == b) | (a == Inf) is equivalent to
c(FALSE, TRUE) | c(TRUE, FALSE)
and the resultant logic array is c(TRUE, TRUE), which gives you TRUE when you apply all over it.

Is there a better way to check if all elements in a list are named?

I want to check if all elements in a list are named. I've came up with this solution, but I wanted to know if there is a more elegant way to check this.
x <- list(a = 1, b = 2)
y <- list(1, b = 2)
z <- list (1, 2)
any(stringr::str_length(methods::allNames(x)) == 0L) # FALSE, all elements are
# named.
any(stringr::str_length(methods::allNames(y)) == 0L) # TRUE, at least one
# element is not named.
# Throw an error here.
any(stringr::str_length(methods::allNames(z)) == 0L) # TRUE, at least one
# element is not named.
# Throw an error here.
I am not sure if the following base R code works for your general cases, but it seems work for the ones in your post.
Define a function f to check the names
f <- function(lst) length(lst) == sum(names(lst) != "",na.rm = TRUE)
and you will see
> f(x)
[1] TRUE
> f(y)
[1] FALSE
> f(z)
[1] FALSE
We can create a function to check if the the names attribute is NULL or (|) there is blank ("") name, negate (!)
f1 <- function(lst1) is.list(lst1) && !(is.null(names(lst1))| '' %in% names(lst1))
-checking
f1(x)
#[1] TRUE
f1(y)
#[1] FALSE
f1(z)
#[1] FALSE
Or with allNames
f2 <- function(lst1) is.list(lst1) && !("" %in% allNames(lst1))
-checking
f2(x)
#[1] TRUE
f2(y)
#[1] FALSE
f2(z)
#[1] FALSE

What is the behavior of the ampersand operator in R's sum(...) function

Below, a line from a script I'm translating from R into Python. I'm more experienced at Python than I am at R, and I'm running into a little trouble here:
val = sum(l & f==v)
Let l be a vector of true/false values. Let f be a vector of trivial values, and v some possible value of f to test against. I expect l and f to be of the same length. The f==v part will also yield a boolean array. Now I am left with the question what the &/ampersand (Logical AND, according to the R documentation) will do in this context. Will the sum() function return the sum of a boolean array that indicates where both the l and f==v boolean arrays are true? Or wil it sum all true values for both arrays and add them up?
Thank you in advance!
Let define several vectors :
l <- c(TRUE, FALSE, TRUE, FALSE, TRUE)
v <- 1:5
f <- rep(c(1, 4), c(3, 2))
now let see what we have when we decompose your line sum(l & f==v):
In this line, == has precedence over &:
fev <- f==v
fev
[1] TRUE FALSE FALSE TRUE FALSE
Then we do l & fev:
lafev <- l & fev
[1] TRUE FALSE FALSE FALSE FALSE
lastly, we sum:
sum(lafev)
[1] 1
The sum tells us how many simultaneous TRUE there are in l and f==v by converting the logical values to numeric: TRUE becomes 1 and FALSE becomes 0. So, in this example, 1.
Here is a sample python version:
l = [True, True, False]
f = [True, False, True]
v = [True, True, True]
f_eq_v = [f[i] == v[i] for i in range(len(f))] # f == v in R
val = sum([l[i] & f_eq_v[i] for i in range(len(l))]) # val = sum(l & f==v) in R

Replace elements of vector by vector

I want to replace few elements of vector by whole second vector. Condition is, that replaced elements of first vector are equal to third vector. Here is an example:
a <- 1:10
b <- 5:7
v <- rnorm(2, mean = 1, sd = 5)
my output should be
c(a[1:4], v, a[8:10])
I have already tried
replace(a, a == b, v)
a[a == b] <- v
but with a little success. Can anyone help?
The == operator is best used to match vectors of the same length, or when one of the vector is only length 1.
Try this, and notice in neither case do you get the positional match that you desire.
> a == b
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
Warning message:
In a == b : longer object length is not a multiple of shorter object length
> b == a
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
Warning message:
In b == a : longer object length is not a multiple of shorter object length
Instead, use match() - this gives you the index position where there is a match in the values.
> match(b, a)
[1] 5 6 7
Then:
a <- 1:10
b <- 5:7
v <- rnorm(3, mean=1, sd=5)
a[match(b, a)] <- v
The results:
a
[1] 1.0000000 2.0000000 3.0000000 4.0000000 -4.6843669 0.9014578 -0.7601413 8.0000000
[9] 9.0000000 10.0000000
Here' another option:
a[a %in% b] <- v
Since in the example described in the OP there are three common numbers in the vectors a and b while v <- rnorm(2, mean = 1, sd = 5)
contains only 2 numbers, the vector v will be recycled and a warning will be issued.
The warning and recycling can be prevented, e.g., by defining v as
v <- rnorm(sum(a %in% b), mean = 1, sd = 5)

R - compare create logical vector

I have 2 vectors, I want to show logic if elements in vector z are equal to any elements in vector x.
z <- rep(c("AA","AB","AC","AD","AE"), 40)
x <- c("AA","AD","BB")
z == x
I use z == x but the True False values are not correct.
Warning shows, "longer object length is not a multiple of shorter object length"
You are looking for %in% (see ?"%in%" for details):
z %in% x
head(z %in% x)
# [1] TRUE FALSE FALSE TRUE FALSE TRUE

Resources