Sum of functions in R - r

I have a function in R and I wish to take the sum of this function with different values. However, since I have a break condition (made by an if statement) I cannot just do this explicitly:
F<- function(x) if(x<5) 1 else 0
sum(F(seq(1,10,1))
#[1] 1
#Warning message:
#In if (x < 5) 1 else 0 :
# the condition has length > 1 and only the first element will be used
so it is trying to do the sequence of the function and not the sum of the sequence. I wish to avoid the for loop as this can make long codes very cluttered; specifically to avoid ugly nested for loops.
How do I go about this?

You can use Vectorize:
F_v <- Vectorize(F)
sum(F_v(seq(1,10,1)))
# [1] 4

If you like to avoid for-loops, sapply is an option for you, because it is faster.
sapply(seq(1,10,1), FUN <- function(x) {if(x<5) 1 else 0})

Related

Performing operation along vector in R - only returns a single value

I'm trying to perform a conditional operation on a vector xt, given a value lambdat.
Outside of the ifelse() function the operations work, but the full code doesn't. See example below, cheers!
xt <- c(1,2,3)
lambdat <- 1
bc_applied_columnt <- ifelse(lambdat != 0, (xt^(lambdat)-1)/lambdat, log(xt))
This returns 0 (first value in the vector xt), but I'd like it to return the output of (xt^(lambdat)-1)/lambdat or log(xt) - depending on the condition.
ifelse returns the output of the same length as the condition that you check. Since length(lambdat != 0) is of length 1 ifelse returns output of length 1 as well. When you have only one value to check use if/else.
xt <- c(1,2,3)
lambdat <- 1
if(lambdat != 0) (xt^(lambdat)-1)/lambdat else log(xt)
#[1] 0 1 2

Can I further vectorize this function

I am relatively new to R, and matrix-based scripting languages in general. I have written this function to return the index's of each row which has a content similar to any another row's content. It is a primitive form of spam reduction that I am developing.
if (!require("RecordLinkage")) install.packages("RecordLinkage")
library("RecordLinkage")
# Takes a column of strings, returns a list of index's
check_similarity <- function(x) {
threshold <- 0.8
values <- NULL
for(i in 1:length(x)) {
values <- c(values, which(jarowinkler(x[i], x[-i]) > threshold))
}
return(values)
}
is there a way that I could write this to avoid the for loop entirely?
We can simplify the code somewhat using sapply.
# some test data #
x = c('hello', 'hollow', 'cat', 'turtle', 'bottle', 'xxx')
# create an x by x matrix specifying which strings are alike
m = sapply(x, jarowinkler, x) > threshold
# set diagonal to FALSE: we're not interested in strings being identical to themselves
diag(m) = FALSE
# And find index positions of all strings that are similar to at least one other string
which(rowSums(m) > 0)
# [1] 1 2 4 5
I.e. this returns the index positions of 'hello', 'hollow', 'turtle', and 'bottle' as being similar to another string
If you prefer, you can use colSums instead of rowSums to get a named vector, but this could be messy if the strings are long:
which(colSums(m) > 0)
# hello hollow turtle bottle
# 1 2 4 5

Creating a Pure function in R

I'm used to using Pure functions in Mathematica. How might I use them in R? For example:
Given a list of numbers, I want to assign TRUE/FALSE depending on whether the number is positive/negative.
z <- do.call(rnorm,list(n=10)) # Generate 10 numbers
f <- function(x) { x > 0 ? TRUE : FALSE } # Searching for proper syntax
b <- lapply(z,f)
Thanks
Narrowly translated, your function would be:
f <- function(x) { if (x > 0) TRUE else FALSE }
(you don't need to use ifelse() because this is a context in which x will be a scalar (i.e., a length-1 vector))
f <- function(x) { x > 0 }
would give the same result in your lapply call: so would
lapply(z,">",0)
As commented above you could use ifelse(z>0,TRUE,FALSE).
But there's no need to specify logical return values, because the result of z>0 is already a logical vector. The idiomatic way to do this would be
z <- rnorm(10) ## no need for do.call() in this example
z > 0
(logical comparison is vectorized in R)
Really easy:
b = z > 0
Most simple operations in R are already vectorized.

Calculate a geometric progression

I'm using brute force right now..
x <- 1.03
Value <- c((1/x)^20,(1/x)^19,(1/x)^18,(1/x)^17,(1/x)^16,(1/x)^15,(1/x)^14,(1/x)^13,(1/x)^12,(1/x)^11,(1/x)^10,(1/x)^9,(1/x)^8,(1/x)^7,(1/x)^6,(1/x)^5,(1/x)^4,(1/x)^3,(1/x)^2,(1/x),1,x,x^2,x^3,x^4,x^5,x^6,x^7,x^8,x^9,x^10,x^11,x^12,x^13,x^14,x^15,x^16,x^17,x^18,x^19,x^20)
Value
but I would like to use an increment loop just like the for loop in java
for(integer I = 1; I<=20; I++)
^ is a vectorized function in R. That means you can simply use x^(-20:20).
Edit because this gets so many upvotes:
More precisely, both the base parameter and the exponent parameter are vectorized.
You can do this:
x <- 1:3
x^2
#[1] 1 4 9
and this:
2^x
#[1] 2 4 8
and even this:
x^x
#[1] 1 4 27
In the first two examples the length-one parameter gets recycled to match the length of the longer parameter. Thats why the following results in a warning:
y <- 1:2
x^y
#[1] 1 4 3
#Warning message:
# In x^y : longer object length is not a multiple of shorter object length
If you try something like that, you probably want what outer can give you:
outer(x, y, "^")
# [,1] [,2]
#[1,] 1 1
#[2,] 2 4
#[3,] 3 9
Roland already addressed the fact that you can do this vectorized, so I will focus on the loop part in cases where you are doing something more that is not vectorized.
A Java (and C, C++, etc.) style loop like you show is really just a while loop. Something that you would like to do as:
for(I=1, I<=20, I++) { ... }
is really just a different way to write:
I=1 # or better I <- 1
while( I <= 20 ) {
...
I <- I + 1
}
So you already have the tools to do that type of loop. However if you want to assign the results into a vector, matrix, array, list, etc. and each iteration is independent (does not rely on the previous computation) then it is usually easier, clearer, and overall better to use the lapply or sapply functions.

append results of loop into numeric vector

I would like to create a numeric vector with the results of a loop such as
> for (i in 1:5) print(i+1)
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
It seems strange that the same expression without 'print' returns nothing
> for (i in 1:5) i+1
>
Does anyone have an explanation/solution?
This is standard behaiviour -- when you say you want to create a numeric vector,
print will not do that
The expression in a for loop is an argument to the primitive function for
From ?`for` in the value section
for, while and repeat return NULL invisibly. for sets var to the last
used element of seq, or to NULL if it was of length zero.
print prints the results to the console.
for(i in 1:5) i + 1
merely calculates i + 1 for each iteration and returns nothing
If you want to assign something then assign it using <-, or less advisably assign
You can avoid an explicit loops by using sapply. This (should) avoid any pitfalls of growing vectors
results <- sapply(1:5, function(i) { i + 1})
Now frankly, there must be a better solution than this
loopee <- function(x){
res <- vector(mode = "numeric", length(x))
for (i in 1:x) {res[i] <- i+1}
return(res)}
> loopee(5)
[1] 2 3 4 5 6

Resources