Skip to content
Back to formatted view

Raw Message

Message-ID: <1384699803079-4680621.post@n4.nabble.com>
Date: 2013-11-17T14:50:03Z
From: Stageexp
Subject: Apply function to one specific column / Alternative to for loop
In-Reply-To: <1384626267.51320.YahooMailNeo@web142601.mail.bf1.yahoo.com>

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.