[R] help with tapply or other apply
Patrick Burns
pburns at pburns.seanet.com
Sun May 2 13:00:27 CEST 2010
You don't show how you are doing it with
a 'for' loop, but I suspect that you just
need to eliminate the subscript you are
using for rows.
For example:
for(i in 1:nrow(data)) {
data$z[i] <- data[i, 'x'] + data[i, 'y']
}
can be written more simply and much more
efficiently as:
data$z <- data[, 'x'] + data[, 'y']
Using an "apply" function is not going to
improve the efficiency. This is the subject
of Circles 3 and 4 of 'The R Inferno'.
On 02/05/2010 11:26, peterko wrote:
>
> Hi, my data looks this:
> id forma program kod obor
> rocnik
> 1 10001 kombinovaná Matematika M1101 matematika 1
> 2 10002 prezenční Informatika N1801 teoretická informatika 1
> 3 10002 prezenční Informatika B1801 obecná informatika 3
> 4 10003 prezenční Informatika M1801 softwarové systémy 5
> 5 10004 prezenční Informatika B1801 obecná informatika 2
> 6 10005 kombinovaná Informatika P1801 diskrétní modely a algoritmy 2
> stav ukrok
> 1 zanechal 2002/2003
> 2 studuje
> 3 absolvoval 2008/2009
> 4 absolvoval 2005/2006
> 5 zanechal 2007/2008
> 6 zanechal 2004/2005
>
> data$ukrok is a factor
> data$rocnik is numeric
>
> I want to create new column (data$z) and in this column have to be
> as.numeric(first 4 char of column(data$ukrok))-data$rocnik ---- by the
> rows
> If ukrok is empty it means 2009.
> I know how to do it by cycle FOR , but this is not rigth way. I have too
> many observation, and this way is soo slowly.
> Know someone how to do it using function TAPPLY ? or another apply function
> ???
--
Patrick Burns
pburns at pburns.seanet.com
http://www.burns-stat.com
(home of 'Some hints for the R beginner'
and 'The R Inferno')
More information about the R-help
mailing list