I have 2 columns of different size, A and B.
A | B
5 | 4
1 | 2
3 |
How do I say If CellA is < CellB then give me CellB-CellA? So it calculates:
"5(A1) is bigger than 4(B1) and 2(B2)" so no results
"1(A2) is smaller than 4(B1) and 2(B2)" so we have 3 and 1
"3(A3) is smaller than 4(B1) and bigger than 2(B2)" so we have 1
Result = (3, 1, 1)
The closest I got was:
if (A < B) {B - A}
But that only works with columns of same size, I want each individual cell of Column A to interact with each individual cell of column B. How can I do that?
Since the columns of different sizes then it must be list and not data.frame so the solution :
listAB <- list(A = c(5,1,3) , B = c(4,2))
equ <- function(li){
result <- vector("numeric")
for (x in li$A){
result <- append(result , sapply(li$B , function(y) if(x < y) y - x))
}
unlist(result)
}
equ(listAB)
#> [1] 3 1 1
Created on 2022-05-29 by the reprex package (v2.0.1)
who can tell me a solution to avoid a result of 0 in y in R program
y= 1-(1/(1+exp(-x)))
exp(-x) is > 0 but for x values like 100 the result of y is 0.
I need to get small numbers > 0 to apply log(y) without getting -Inf result.
You can solve this with some simple maths:
x <- 1
1-(1/(1+exp(-x)))
#[1] 0.2689414
1/(exp(x) + 1)
#[1] 0.2689414
x <- 100
1-(1/(1+exp(-x)))
#[1] 0
1/(exp(x) + 1)
#[1] 3.720076e-44
If you are interested in log(y) for large values of x, just ignore the + 1, y is approximately exp(-x) with a very small error for large x. Thus, log(y) == -x for large x.
Your teacher probably wants you to do the maths.
I am a new R user and have very limited programming experience, hence my question and poorly written code.
I was assigned a problem where I had to use a while loop to generate the numbers of the Fibonacci sequence that are less than 4,000,000 (the Fibonacci sequence is characterized by the fact that every number after the first two is the sum of the two preceding ones).
Next, I had to compute the sum of the even numbers in the sequence that was generated.
I was successful with my response, however, I don't think the code is written very well. What could I have done better?
> x <- 0
> y <- 1
> z <- 0
if (x == 0 & y == 1) {
cat(x)
cat(" ")
cat(y)
cat(" ")
while (x < 4000000 & y < 4000000) {
x <- x + y
cat(x)
cat(" ")
if (x %% 2 == 0) {
z <- x + z
}
y <- x + y
cat(y)
cat(" ")
if (y %% 2 == 0) {
z <- y + z
}
}
}
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 5702887 9227465
cat(z)
4613732
First of all, cat comes with a sep argument. You can do cat(x, y, sep = " ") rather than using 3 lines for that.
Secondly, when you call while (x < 4000000 & y < 4000000) note that y will always be greater than x because it is the sum of the last x and y ... so it should suffice to check for y < 4000000 here.
For the while loop, you could also use a counter - might be more intuitive. Indexing in R isn't that fast though
fib <- c(0, 1)
i <- 2
while (fib[i] < 4000000) {
fib <- c(fib, fib[i-1] + fib[i])
i <- i + 1
}
sum(fib[fib %% 2 == 0])
If you don't necessarily need the while, you could also approach it via recursion
fib <- function(x, y) {
s <- x + y
c(s, if (s < 4000000) fib(y, s))
}
f <- fib(0, 1)
sum(f[f %% 2 == 0])
First, there's no need o explicitly print everything out.
Second, it's more idiomatic in R to make a vector of the Fibonacci numbers and then sum. If you don't know an explicit closed form for the Fibonacci numbers, or if you've been told not to use this, then use a loop to create the list of Fibonacci numbers.
So to construct the list of Fibonacci numbers (two at a time) you can do
x <- 0
y <- 1
fib <- c()
while (x < 4000000 & y < 4000000){
x <- x + y
y <- x + y
fib = c(fib, x, y)
}
This will give you a vector of Fibonacci numbers, containing all those less than 4000000 and a few more (the last element is 9227465).
Then run
sum(fib[fib %% 2 == 0 & fib < 4000000])
to get the result. This returns 4613732, like your code does. The subsetting operator [], when you put a logical condition inside it, will output just those numbers which satisfy the logical condition -- in this case, that they're even and less than 4000000.
I am using the closed form of the fibonacci sequence as found here
fib = function(n) round(((5 + sqrt(5)) / 10) * (( 1 + sqrt(5)) / 2) ** (1:n - 1))
numbers <- 2
while (max(fib(numbers)) < 4000000){ # try amount of numbers while the maximum of the sequence is less than 4000000
sequence <- fib(numbers) # here the sequence that satisfies the "4000000 condition will be saved"
numbers <- numbers + 1 # increase the amount of numbers
}
total_sum <- sum(sequence[sequence%%2==0]) # summing the even numbers
This is how I would do it. First, I defined a global variable i to include the first two elements of the Fibonacci series. Then at the end, I re-assigned the global variable to its initial value (i.e. 1). If I don't do that, then when I call the function fib(0,1) again, the output is incorrect as it calls the function with the last value of i. It's also important to do return() to ensure it doesn't return anything in the else clause. If you don't specify return(), the final output will be 1, instead of the Fibonacci series.
Please note the series only goes till the number 13 (z<14) obviously you can change that to whatever you want. May also be a good option to include this as the third argument of the function, something like fib(0,1,14). Try it out!
i <<- 1
fib <- function(x,y){
z <- x+y
if(z<14){
if (i==1){
i <<- i+1
c(x,y,z,fib(y,z))
}
else c(z, fib(y,z))
}
else {
i <<- 1
return()
}
}
a <- fib(0,1)
a
When providing a default argument to an R function, this argument is evaluated when first used in the function. How is it possible to evaluate default argument earlier in an elegant way? Example:
f <- function(x, y = 2 * x)
{
if(x < 0) x = 10
y
}
f(1) ## Returns 2
f(-1) ## Returns 20 but I would like it to return -2
Thanks
The answer wasn't to hard to find. The function 'force' does the trick:
f <- function(x, y = 2 * x)
{
force(y)
if(x < 0) x = 10
y
}
f(1) ## Returns 2
f(-1) ## -2
R has the function outer that allows you to compute a function for every combination of inputs xs and ys, e.g.:
xs <- 1:5
ys <- 0:10
zs <- outer(xs, ys, Vectorize(function(x, y) sin(x) + y**2))
Let's say I want to know the minimal value in zs:
> min(zs)
[1] -0.9589243
and what position it's in:
> which(zs==min(zs))
[1] 5
Now how can I figure out which x of xs and which y of ys produced the 5th z of zs?
I can recompute using something like outer(xs, ys, Vectorize(function(x, y) paste(x,y)))[which(zs==min(zs))] to get a string of the indices, but this seems bloody inefficient to me. What's a better way?
You have to use:
which(zs==min(zs), arr.ind=T)
# row col
#[1,] 5 1
You can use arr.index=TRUE
which(zs==min(zs),arr.ind = TRUE)
One can use expand.grid:
> expand.grid(xs, ys)[zs==min(zs),]
Var1 Var2
5 5 0