@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.
Hello Bert and Avi,Sorry, it is typo. it should be:
for (i in colnames(df)){
? ......
}
below is the code I'm currently using
try2.un$ab2 <-
? ifelse(grepl("ab2",try2.un$data1), try2.un$data1,
? ? ? ? ?ifelse(grepl("ab2",try2.un$data2), try2.un$data2,
? ? ? ? ? ? ? ? ifelse(grepl("ab2",try2.un$data3), try2.un$data3,
? ? ? ? ? ? ? ? ? ? ? ?ifelse(grepl("ab2",try2.un$data4),
try2.un$data4,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ifelse(grepl("ab2",try2.un$data5),
try2.un$data5,NA
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ) ) ) ) )
As you can see, it uses 5 fields (data1 -- 5 ) in ifelse function. I want to
turn it to for loop, because the number of data(s) fields is dynamic. In this
sample is 5, But it maybe more than 15 in some of situation. So, I want use loop
to solve it and avoid to write those many ifelse statement. Also, in try2.un
data frame, there are many other fields that I don't need to use in the
loop.?
I'm not sure if the loop is a correct solution. But I'm willing to learn
any more suggestion from you.
Thanks,
Kai
On Tuesday, November 15, 2022 at 09:23:03 AM PST, avi.e.gross at gmail.com
<avi.e.gross at gmail.com> wrote:
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.
______________________________________________
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]]