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]]
There is no function columns() in base R. Is 'date' an object that contains column names or indices? Or do you mean that it is a string that appears in the column names you wish to loop over? A small reprex should be easy to provide to show us explicitly what you want. Please do so -- unless, of course, someone with greater insight than I provides you a satisfactory answer. Cheers, Bert On Tue, Nov 15, 2022 at 8:19 AM Kai Yang via R-help <r-help at r-project.org> wrote:> 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 at 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. >[[alternative HTML version deleted]]
@vi@e@gross m@iii@g oii gm@ii@com
2022-Nov-15 17:22 UTC
[R] add specific fields in for loop
Kai, As Bert pointed out, it may not be clear what you want. As a GUESS, you have some arbitrary data.frame object with multiple columns and you want to do something on selected columns. Consider changing your idea to be in several stages for simplicity and then optionally later rewriting it. So step 1 is to get a vector of column names. The normal way to do this in base R is not with a function called columns(df) but colnames(df) ... Step 2 is to use one of many techniques that take that vector of names and select the ones you want to keep. In base R there are many ways to do that including using regular expressions as in the "grep" family of functions. You may end up with a new vector of names perhaps shorter or in a different order. Step 3 is to use those names in your loop. If you want say to convert a column from character to numeric, and your loop index is "current" you might write something like: df[current] <- as.numeric(df[current]) There are many ways and it depends on what exactly you want to do. There are packages designed to make some of these things fairly simple, such as dplyr where you can ask to match names that start or end a certain way or that are of certain types. Avi -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of Kai Yang via R-help Sent: Tuesday, November 15, 2022 11:18 AM To: R-help Mailing List <r-help at r-project.org> Subject: [R] add specific fields in for loop 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 at 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.
?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 at 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