[R] add specific fields in for loop

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Tue Nov 15 19:53:58 CET 2022


Às 16:18 de 15/11/2022, Kai Yang via R-help escreveu:
> Hi Team,
> I can write a for loop like this:
> for (i in columns(df)){
>    ......
> }
> 
> But it will working on all column in dataframe df. If I want to work on some of specific fields (say: the fields' name content 'date'), how should I modify the for loop? I changed the code below, but it doesn't work.
> for (i in columns(df) %in% 'date' ){
>    .....
> }
> 
> 
> Thank you,
> Kai
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Hello,

Something like this?


for(i in grep("date", names(df))) {
   #
}


Hope this helps,

Rui Barradas



More information about the R-help mailing list