[R] Apply function to one specific column / Alternative to for loop

Stageexp ronjaq at gmx.net
Sun Nov 17 15:50:03 CET 2013


Hi,
Try:
indx <- grep("Test",test_df[,1])  ##assuming that there is some pattern
 res <- within(test_df[-indx,],titel <- rep(test_df$titel[indx],
diff(c(indx,nrow(test_df)+1))-1))

## If you need to change the class

res[] <- lapply(res,function(x) if(any(grepl("[[:alpha:]]",x)))
as.character(x) else as.numeric(as.character(x)))


##Using data.frame(cbind()), etc. creates 


A.K.


This option worked great for me! I knew there was a nicer and much faster
way to solve this. One thing I already learned about R: Never use for-loops,
there is always a better way :-)



--
View this message in context: http://r.789695.n4.nabble.com/Apply-function-to-one-specific-column-Alternative-to-for-loop-tp4680566p4680621.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list