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.
Related
I am new to R (also not too good at math) and I am trying to calculate this equation in R with some difficulties:
X is some integer data I have, with 550 samples.
Any help is appreciated since I am unsure how to do this. I think I have to use a for loop and the sum() function but other than that I don;t know.
R supports vectorisation, which means you very rarely need to implement for loops.
For example, you can solve your equation like so:
## I'm just making up a long numerical vector for x - obviously you can use anything
x <- 1:1000
solution <- sum(20/x)^0.5
Unless the brackets denote the integral, rather than the sum? In which case:
solution <- sum( (20/x)^0.5 )
I'm new to R and am struggling with the apply function. It is really slow to execute and I was trying to optimize some code I received.
I am trying to do some matrix operations (element-wise multiplication and division on ~10^6 element matrices) then sum the rows of the resulting matrix. I found the fantastic library Rfast and it executes what I thought was the same code in about 1/30 the time, but I am getting systematic differences between my 'optimized' answer and the previous answer.
The original code was something along the lines of
ans <- apply(object, 1, function(x) sum((x - a) / b))
and my code is
ans = Rfast:::rowsums((object-a)/b)
I'm not sure if it's because one of the methods is throwing away precision or making rounding errors - any thoughts?
Edit
Trying to reproduce the error is pretty hard...
I have been able to isolate the discrepancy to when I divide by my vector b with entries each ~ 3000 (i.e. [3016.460436, 3021.210321, 3033.3303219]. If I take this term out the two methods give the same answer.
I then tried two methods to improve my answer, one was dividing b by 1000 then dividing the sum by 1000 at the end. This didn't work, presumably because the float precision is the same either way.
I also tried forcing my b vector to be integers, which also didn't work.
Sample data doesn't reproduce my error either, which is frustrating...
objmat = rbind(rep(c(1,0,0),1000),rep(c(0,0,1),1000))
amat = rbind(rep(c(0.064384654, 0.025465132, 0.36543214),1000))
bmat = rbind(rep(c(1016.460431,1021.210431,1033.330431),1000))
ans = apply(objmat,1,function(x) sum((x-amat)/bmat))
gives
ans[1] = 0.5418828413
rowsums((objmat[1,]-amat)/bmat) = 0.5418828413
I think it has to be a floating point precision error, but I'm not sure why my dummy data doesn't reproduce it, or which method (apply or rowsums) would be more accurate!
I was trying to solve a basic matrix problem. .
I used :
A<- matrix(c(2,7,5,7), 2,2)
b<- c(8,12)
solve(A,b, fractions = TRUE)
However, my result only gives me results in decimal places. How can get fractions results?
I also want to plot this equation above.
I used:
plotEqn(A,b)
However, it tells me this equation can't be found. Can I have some advice please?
Thank you very much!!!
For your first question,
MASS::fractions(solve(A,b))
gives {4/21, 32/21} (note that you won't always be guaranteed the correct answer, as R does floating-point calculation unlike e.g. Mathematica)
For your second question, it looks like the plotEqn() function is in the matlib package: if you have that package installed, then either first loading the package (with library("matlib")) or matlib::plotEqn(A,b) should work.
On closer inspection it looks like you want matlib::Solve() for the first question (note that R is case-sensitive, so solve and Solve are different):
library(matlib)
Solve(A,b, fraction=TRUE)
## x1 = 4/21
## x2 = 32/21
I have a dataframe in which I want to add a new column which is the product of two other columns, divided by 100.
The command I'm trying to use is:
fulldata$Conpolls <- fulldata$Conprct/100 * fulldata$Total.seats
for which I receive:
Error: unexpected input in "full.data$Conpolls <- fulldata$Conprct /100 * fulldata$Total.seats"
When I try to break up the process in 2 steps as:
fulldata$Conpolls <- fulldata$Conprct * fulldata$Total.seats
I get the error:
non-numeric argument to binary operator.
Any tips or help from experienced users greatly appreciated!
Veerendra Gadekar's answer should be correct if all the columns are numeric values.
If the columns with which you are doing the operations are not guaranteed to be numeric, you may turn them into numeric values with as.numeric(). It should look like this:
fulldata$Conpolls <- (as.numeric(fulldata$Conprct) * as.numeric(fulldata$Total.seats))/100
fulldata$Conpolls <- (fulldata$Conprct * fulldata$Total.seats)/100
This doesn't answer the question, however this should be the proper syntax to write such arithmetic operations. And yes as mentioned in the comments you should check the class of the objects you are using to find out what is wrong
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.