[R] Question with "apply"

David Carlson dcarlson at tamu.edu
Thu May 9 22:30:10 CEST 2013


The apply() function works on an array or a matrix. There is no need to
guess, just read the manual page:

? apply

So including a character variable forces the entire matrix to characters.
Excluding character variables will let you operate on the numeric values,
but your code suggests you are trying to create a new variable based on a
single column so apply() is the wrong tool. Without a working example, it is
not possible to be more specific. Including a sample of your data.frame with
dput() would make that possible.

-------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77840-4352


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Pooya Lalehzari
Sent: Thursday, May 9, 2013 10:51 AM
To: r-help at r-project.org
Subject: [R] Question with "apply"

Hello,
When I use "apply" on a data frame, it seems like I get an error when I have
a column that is not numeric. Via trial and error I realized that if I
remove that column, I can get it to run. Is there a better way to tell the
function not to worry about the character columns, especially since I am not
even trying to do any calculations on it?
R_Cat is the character column that is causing error and I am trying to do
calculations on t5R.

Data_F_Cat$t5R_Cat = apply(Data_F[,names(Data_F) != "R_Cat"],1,function(row)
{
    ifelse( abs(row["t5R"]) <Thresh1, 0,
    ifelse( abs(row["t5R"]) <Thresh2, ifelse( row["t5R"] <0, -1, 1),
    ifelse( abs(row["t5R"]) <Thresh3, ifelse( row["t5R"] <0, -2, 2),
    ifelse( abs(row["t5R"]) <Thresh4, ifelse( row["t5R"] <0, -3, 3),
    ifelse( abs(row["t5R"]) <0, -4, 4)))))
})


Thank you,
Pooya.


THIS E-MAIL IS FOR THE SOLE USE OF THE INTENDED
RECIPIENT(S) AND MAY CONTAIN CONFIDENTIAL AND PRIVILEGED INFORMATION.

ANY UNAUTHORIZED REVIEW, USE, DISCLOSURE OR DISTRIBUTION IS PROHIBITED. IF
YOU ARE NOT THE INTENDED RECIPIENT, PLEASE CONTACT THE SENDER BY REPLY
E-MAIL AND DESTROY ALL COPIES OF THE ORIGINAL E-MAIL.




	[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org 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.



More information about the R-help mailing list