Displaying 2 results from an estimated 2 matches for "mysquare".
2012 Apr 05
3
Apply function to every 'nth' element of a vector
Dear R users,
how do I e.g. 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
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",