[R] help with tapply or other apply

Jim Lemon jim at bitwrit.com.au
Sun May 2 13:23:54 CEST 2010


On 05/02/2010 08:26 PM, 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
> ???
Hi Peterko,
You can do this with vector calculation like this:

# make sure that ukrok is character, not factor
data$ukrok<-as.character(data$ukrok)
# set the empty elements to the correct value
data$ukrok[nchar(data$ukrok)==0]<-"2009/2009"
# split ukrok, unlist it, take only the first component
# convert to numeric and subtract rocnik
data$z<-as.numeric(matrix(unlist(strsplit(data$ukrok,"\")),
  ncol=2,byrow=TRUE)[,1])-data$rocnik

Jim



More information about the R-help mailing list