Displaying 3 results from an estimated 3 matches for "applyfun".
2012 Apr 05
3
Apply function to every 'nth' element of a vector
.... square each second element of a vector with an even
number of elements? Or more generally to apply a function to every
'nth' element of a vector. I looked into the apply functions, but
found no hint.
For example:
v <- c(1, 2, 3, 4)
mysquare <- function (x) { return (x*x) }
w <- applyfun(v, mysquare, 2)
then w should be c(1, 4, 3, 16)
Thanks for your time,
Michael Bach
2009 Dec 16
1
difference between the meaning of MARGIN in sweep() and apply()
For example, subtracting 1:2 from the rows of a two-column matrix:
> t(apply(matrix(1:6,ncol=2),MARGIN=1,function(y) y - 1:2))
[,1] [,2]
[1,] 0 2
[2,] 1 3
[3,] 2 4
> sweep(matrix(1:6,ncol=2),MARGIN=2,1:2,FUN="-")
[,1] [,2]
[1,] 0 2
[2,] 1 3
[3,] 2 4
Is there a logic to this difference, or is it just a quirk of the history of
these
2012 Mar 25
2
avoiding for loops
I have data that looks like this:
> df1
group id
1 red A
2 red B
3 red C
4 blue D
5 blue E
6 blue F
I want a list of the groups containing vectors with the ids. I am
avoiding subset(), as it is
only recommended for interactive use. Here's what I have so far:
df1 <- data.frame(group=c("red", "red", "red", "blue",