I have a function to clear my data and keep only the observations that I want but that includes many if statements. I have an order for the codes I want to keep by id.
clear.data = function(x)
{
A = unique(x$code)
if (4 %in% A )
{x = subset(x,code==4)}
else if (10404 %in% A)
{x = subset(x,code==10404)}
else if (3942 %in% A)
{x = subset(x,code==3942)}
else {x=x}
return(x)
}
For example in the data x
x = data.frame(id = c("A","A", "A", "B", "B","B", "B","C","C", "C","C"),
date = c( "29/05/2013", "23/08/2011", "25/09/2011", "18/11/2011", "10/07/2013", "04/10/2011", "10/11/2011",
"15/12/2011", "10/02/2008", "07/09/2009", "22/03/2012" ),
code = c(4,4,3942,4,10404,3942,10404,10404,3942,10404,3942) )
I will use lapply to keep only the observations by person I am interested
> lapply(split(x,x$id),clear.data)
$A
id date code
1 A 29/05/2013 4
2 A 23/08/2011 4
$B
id date code
4 B 18/11/2011 4
$C
id date code
8 C 15/12/2011 10404
10 C 07/09/2009 10404
The problem is that I have 150 codes so that's a lot of if statements and a large dataset to apply my function. Is there a way to reduce the if statements somehow? I busted my head in order to find a solution and have searched a lot but couldn't find anything. Do you have any ideas? Many thanks
Aucun commentaire:
Enregistrer un commentaire