Hello,

I am trying to map a factor variable within a data frame to a new variable 
whose entries are derived from the content of the original variable and there 
are fewer factors in the new variable. That is, I'm trying to set up a 
surjection.

After first thinking that this would be a common operation and would have a 
quite simple interface, I can not seem to find one, nor any similar posts on 
this topic (please correct me if there is something).

Therefore, I have written a function to perform this mapping. However, the 
function I have written doesn't seem to work with vectors greater than length 
1, and as such is useless. Is there any way to ensure the function would work 
appropriately for each element of the vector input?

mapLN <- function(x)
{
        Reg <- levels(df$Var1)
        if (x==Reg[1] | x==Reg[2] | x==Reg[13] | x==Reg[17] | x==Reg[20] | 
x==Reg[23] | x==Reg[27]) {"North"} else
        if (x==Reg[3] | x==Reg[5] | x==Reg[7] | x==Reg[14] | x==Reg[15] | 
x==Reg[24] | x==Reg[30]) {"East"} else
        if (x==Reg[4] | x==Reg[6] | x==Reg[8] | x==Reg[9] | x==Reg[11] | 
x==Reg[16] | x==Reg[18] | x==Reg[21] | x==Reg[22] | x==Reg[25] | x==Reg[28] | 
x==Reg[29] | x==Reg[31]) {"West"} else
        if (x==Reg[10] | x==Reg[12] | x==Reg[19] | x==Reg[26] | x==Reg[32]) 
{"South"} else
        stop("Not in original set")     
}

Many thanks,
James

This E-Mail is confidential and intended solely for the use of the individual 
to whom it is addressed.  If you are not the addressee, any disclosure, 
reproduction, copying, distribution or other dissemination or use of this 
communication is strictly prohibited.  If you have received this transmission 
in error please notify the sender immediately by replying to this e-mail, or 
telephone 01382 207 222, and then delete this e-mail.

All outgoing messages are checked for viruses however no guarantee is given 
that this e-mail message, and any attachments, are free from viruses.  You are 
strongly recommend to check for viruses using your own virus scanner.  Neither 
SCRC or SSSC will accept responsibility for any damage caused as a result of 
virus infection.

______________________________________________
R-help@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.

Reply via email to