Às 19:39 de 15/11/2022, Kai Yang escreveu:
Hello Rui,
Yes, it should be the one I want. I modified the code as:
for(i in grep("data", names(df))) {
try2.un$ab2 <-
ifelse(grepl("ab2", [i]), [i], NA)
}
But I got error message:
Error: unexpected '[' in:
" try2.un$ab2 <-
ifelse(grepl("ab2",["
I think I use [i] in a wrong way. Do you have any suggestion?
Thanks,
Kai On Tuesday, November 15, 2022 at 10:54:08 AM PST, Rui Barradas
<ruipbarra...@sapo.pt> wrote:
À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@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
Hello,
For what I understand of the problem any of the two below does what the
posted code seems to be trying to do. The first is better, no ifelse.
for(i in grep("data", names(df))) {
is.na(try2.un$ab2) <- !grepl("ab2", df[[i]])
}
for(i in grep("data", names(df))) {
try2.un$ab2 <- ifelse(grepl("ab2", df[[i]]), df[[i]], NA)
}
Hope this helps,
Rui Barradas
______________________________________________
R-help@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.