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

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

indx <- grep("Test",test_df[,1])  ##assuming that there is some pattern
 res <- within(test_df[-indx,],titel <- rep(test_df$titel[indx],

## 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 


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