I'm trying to create a vector in R using the rep() function
p <- .9
n <- 100
rep(8,n*(1-p)^2) # expect 8
What is causing the unexpected behavior?
The reason for this is in the comments to the question. A workaround is using:
rep(8, round(n*(1-p)^2))
Condensing the comments. The second argument of rep should be an integer. From the help page: ?as.integer, we know that real numbers are truncated towards zero. So
n*(1-p)^2
is passed to
as.integer(n*(1-p)^2)
which is equal to 0.
Related
I want to multiply a this number 4.193215e+12 with a dataframe. My code is
df <- cbind(Dataset = df$Dataset, df[,2:4] * 4.193215e^12
However an error appears. What is the proper way to code this number 4.193215e+12 in R?
While this is found in the not-quite-obvious location ?NumericConstants , I am hard-pressed to think of a language in which Xe^Y is syntactically correct. Always use either e or ^ for powers.
I am stuck with a problem in R.
It is about removing NAs within vectors and dataframes.
I am given the library, data frame and the vector as follows:
library(dslabs)
data(na_example)
ind <- is.na(na_example)
So, I need to compute the mean, but with the entries that are not NA inside the vector "ind".
I have tried everything, including the answer (I think) that is: mean(!ind), because I HAVE to use the ! operator.
The result is 0.855. However, the evaluating system does not give me a positive score.
Please, could you give me a hand?
You're looking for na.omit, not is.na:
library(dslabs)
data(na_example)
ind <- na.omit(na_example)
mean(ind)
Which gives you: 2.301754
So, I finally got after many hours of struggle.
I was putting the ! in the wrong place
ind <- is.na(na_example)
mean(!ind)
[1] 0.855
It should be:
ind <- !is.na(na_example)
mean(ind)
[1] 0.855
I've tried a couple ways of doing this problem but am having trouble with how to write it. I think I did the first three steps correctly, but now I have to fill the vector z with numbers from y that are divisible by four, not divisible by three, and have an odd number of digits. I know that I'm using the print function in the wrong way, I'm just at a loss on what else to use ...
This is different from that other question because I'm not using a while loop.
#Step 1: Generate 1,000,000 random, uniformly distributed numbers between 0
#and 1,000,000,000, and name as a vector x. With a seed of 1.
set.seed(1)
x=runif(1000000, min=0, max=1000000000)
#Step 2: Generate a rounded version of x with the name y
y=round(x,digits=0)
#Step 3: Empty vector named z
z=vector("numeric",length=0)
#Step 4: Create for loop that populates z vector with the numbers from y that are divisible by
#4, not divisible by 3, with an odd number of digits.
for(i in y) {
if(i%%4==0 && i%%3!=0 && nchar(i,type="chars",allowNA=FALSE,keepNA=NA)%%2!=0){
print(z,i)
}
}
NOTE: As per #BenBolker's comment, a loop is an inefficient way to solve your problem here. Generally, in R, try to avoid loops where possible to maximise the efficiency of your code. #SymbolixAU has provided an example of doing so here in the comments. Having said that, in aid of helping you learn the ins-and-outs of loops and vectors, here's a solution which only requires a change to one line of your code:
You've got the vector created before the loop, that's a good start. Now, inside your loop, you need to populate that vector. To do so, you've currently got print(z,i), which won't really do too much. What you need to to change the vector itself:
z <- c( z, i )
Should work for you (just replace that print line in your loop).
What's happening here is that we're taking the existing z vector, binding i to the end of it, and making that new vector z again. So every time a value is added, the vector gets a little longer, such that you'll end up with a complete vector.
where you have print put this instead:
z <- append(z, i)
I am a novice in R, and so what seemed to work fine in C and Python, surprisingly breaks down in R.
I am trying to calculate the product of the first 1000 Fibonacci numbers. Here is the full code:
#PRRODUCT OF FIBONACCI NUMBERS
Fibonacci<-rep(0, 1000)
Fibonacci[0]<-1
Fibonacci[1]<-1
Product<-1
for (i in 2:1000)
{
Fibonacci[i]<-(Fibonacci[i-1])+(Fibonacci[i-2])
Product<-Fibonacci[i]*Product
}
Fibonacci[1000]
Product
This returns the following error:
Error in Fibonacci[i] <- (Fibonacci[X - 1]) + (Fibonacci[X - 2]) :
replacement has length zero
I am inclined to think I have misunderstood operating with different elements of an array (perhaps the i-2 in the vector description is not correct), but I haven't found anything over the past hour and a half which would have helped me correct it.
So, any insights into the cause of the problem would be most appreciated.
Thank you in advance.
Arrays in R are 1-based.
Fibonacci[1]<-1
Fibonacci[2]<-1
Product<-1
for (i in 3:1000)
{
(the remainder as in your question)
The problem is Fibonacci[0] which is a 0-length numeric. When i = 2, this expression has a right hand side of numeric(0):
Fibonacci[i]<-(Fibonacci[i-1])+(Fibonacci[i-2])
Is there an easy way of avoiding 0 division error in R. Specifically,
a <- c(1,0,2,0)
b <- c(3,2,1,0)
sum(b/a)
This code gives an error due to division by zero.
I would like a way to define anything/0 = 0 so that this kind of operation would still be valid.
Well, let's back up a minute. R does NOT return an error. It quite correctly returns NaN. You had better have a darn good reason for rejecting NaN values in your work. Why do you allow any b element to be zero in the first place? You need to think about what your code is really intended to do, and what it means (statistically, say) to cheerfully throw away all the cases where b[j]==0 .
What is you set all items in the denominator to 0 to NA and then exclude NA? in your sum?
a[a==0] <- NA
sum(b/a, na.rm=TRUE)
#-----
[1] 3.5
Or without modifying a: sum(b/ifelse(a==0,NA,a), na.rm = TRUE)
You could change the function "/" to have an exception for zero:
"/" <- function(x,y) ifelse(y==0,0,base:::"/"(x,y))
For example:
> 10/0
[1] 0
This is very risky though, for example it might break other people's code. If you want to do this it is probably a good idea to assign a different operator rather than changing /. Also it makes mathematically no sense!
If you want to mask all the NaN and Inf results, try something like:
a <- c(1,0,2,0)
b <- c(3,2,1,0)
result <- b/a
sum(result[is.finite(result)])
[1] 3.5
Or all in one line:
sum((b/a)[is.finite(b/a)])
[1] 3.5
I just worked on similar situation.
This works well with ifelse():
sum(ifelse(a==0,0,b/a))