I want to perform the following easy calculation for an example data
a<-seq(1:10)
Now, is there an built-in function,, which returns a vector: (a[1]+a[2],a[3]+a[4],...,a[9]+a[10]). Note I'm able to implement this using a for loop or using rollapply (and deleting some elements). However, I'm wondering if there is a built-in function I do not know so far.
How about this?
a[c(T,F)] + a[c(F,T)]
rollapply in the zoo package can do that in a straightforward manner:
library(zoo)
rollapply(a, 2, by = 2, sum)
Related
Similar questions have been raised for other languages: C, sql, java, etc.
But I'm trying to do this in R.
I have:
ret_series <- c(1, 2, 3)
x <- "ret_series"
How do I get (1, 2, 3) by calling some function / manipulation on x, without direct mentioning of ret_series?
You provided the answer in your question. Try get.
> get(x)
[1] 1 2 3
For a one off use, the get function works (as has been mentioned), but it does not scale well to larger projects. it is better to store you data in lists or environments, then use [[ to access the individual elements:
mydata <- list( ret_series=c(1,2,3) )
x <- 'ret_series'
mydata[[x]]
What's wrong with either of the following?
eval(as.name(x))
eval(as.symbol(x))
Note that some of the examples above wouldn't work for a data.frame.
For instance, given
x <- data.frame(a=seq(1,5))
get("x$a") would not give you x$a.
I need to write a function that does the above, basically. I would like it to be able to apply it to any numeric vector. I am very new to R so I'm struggling to get this off the ground. I appreciate any help!
Rather than write your own function use mean(), which comes with base R:
numbers <- c(11, 3, 4.2, 0, -12)
numbers
result <- mean(numbers)
Similar questions have been raised for other languages: C, sql, java, etc.
But I'm trying to do this in R.
I have:
ret_series <- c(1, 2, 3)
x <- "ret_series"
How do I get (1, 2, 3) by calling some function / manipulation on x, without direct mentioning of ret_series?
You provided the answer in your question. Try get.
> get(x)
[1] 1 2 3
For a one off use, the get function works (as has been mentioned), but it does not scale well to larger projects. it is better to store you data in lists or environments, then use [[ to access the individual elements:
mydata <- list( ret_series=c(1,2,3) )
x <- 'ret_series'
mydata[[x]]
What's wrong with either of the following?
eval(as.name(x))
eval(as.symbol(x))
Note that some of the examples above wouldn't work for a data.frame.
For instance, given
x <- data.frame(a=seq(1,5))
get("x$a") would not give you x$a.
I have a three dimensional data structure reflecting data at particular longitudes, latitudes, and depth. I would like to apply a function to this data. Normally, say I want to find the depth-averaged value I'd do the following:
apply(MyData, MAR = c(1, 2), mean)
which makes sense to me. What I'm struggling with is that I have want to apply a function that depends on longitude and latitude. Is there a way for apply to pass the indices of elements to the function?
I think you want to use outer() and take advantage of lexical scoping
so that you don't have to pass myData to the function being
called with the longitude and lattitude:
myData <- read.table(...) # or whatever
outer(seq.int(dim(mydata)[1]),
seq.int(dim(mydata)[2]),
function(longitude,lattitude){
do things that depend on
myData[longitude,lattitude,]
})
I have a 58 column dataframe, I need to apply the transformation $log(x_{i,j}+1)$ to all values in the first 56 columns. What method could I use to go about this most efficiently? I'm assuming there is something that would allow me to do this rather than just using some for loops to run through the entire dataframe.
alexwhan's answer is right for log (and should probably be selected as the correct answer). However, it works so cleanly because log is vectorized. I have experienced the special pain of non-vectorized functions too frequently. When I started with R, and didn't understand the apply family well, I resorted to ugly loops very often. So, for the purposes of those who might stumble onto this question who do not have vectorized functions I provide the following proof of concept.
#Creating sample data
df <- as.data.frame(matrix(runif(56 * 56), 56, 56))
#Writing an ugly non-vectorized function
logplusone <- function(x) {log(x[1] + 1)}
#example code that achieves the desired result, despite the lack of a vectorized function
df[, 1:56] <- as.data.frame(lapply(df[, 1:56], FUN = function(x) {sapply(x, FUN = logplusone)}))
#Proof that the results are the same using both methods...
#Note: I used all.equal rather than all so that the values are tested using machine tolerance for mathematical equivalence. This is probably a non-issue for the current example, but might be relevant with some other testing functions.
#should evaluate to true
all.equal(log(df[, 1:56] + 1),as.data.frame(lapply(df[, 1:56], FUN = function(x) {sapply(x, FUN = logplusone)})))
You should be able to just refer to the columns you want, and do the operation, ie:
df[,1:56] <- log(df[,1:56]+1)