Hi!
I need to conditionally update a dataframe field based on values in
other fields and can't find even how to search for this right. Sorry
if this has been asked before.
But, specifically, I have a 490 X 221 dataframe and need to count, by
row, how many fields in Dataframe$field_a...Dataframe$field_zz are
non-null and enter this value in Dataset$ABCtaskNum. I have field
name definitions in a vector "vars" and tried writing a custom
function to handle the within-row calculation
myfunct <-function () {for (i in 1:length(vars)) {if (vars[i] != NA)
{Dataset$ABCtaskNum<-Dataset$ABCtaskNum+1}}}
and then use "apply" to handle the row to row calculation
Dataset <- apply(Dataset, 1, myfunc) Where Dataset already has field
Dataset$ABCtaskNum set to 0 in all rows.
But that didn't work. Doesn't help if I declare variables (vars and
ABCtaskNum) in the function declaration either, but then I haven't
quite figured out how best to do variable substitutions in R.
Thanks for any help. Cheers
Jon
Soli Deo Gloria
Jon Erik Ween, MD, MS
Scientist, Kunin-Lunenfeld Applied Research Unit
Director, Stroke Clinic, Brain Health Clinic
Baycrest Centre for Geriatric Care
Assistant Professor, Dept. of Medicine, Div. of Neurology
University of Toronto Faculty of Medicine
Posluns Building, 6th Floor, Room 644
Baycrest Centre for Geriatric Care
3560 Bathurst Street
Toronto, Ontario M6A 2E1
Canada
Phone: 416-785-2500 x3636
Fax: 416-785-2484
Email: [EMAIL PROTECTED]
Confidential: This communication and any attachment(s) may contain
confidential or privileged information and is intended solely for the
address(es) or the entity representing the recipient(s). If you have
received this information in error, you are hereby advised to destroy
the document and any attachment(s), make no copies of same and inform
the sender immediately of the error. Any unauthorized use or
disclosure of this information is strictly prohibited.
[[alternative HTML version deleted]]
______________________________________________
[email protected] mailing list
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.