Skip to content
Prev 334102 / 398513 Next

Need help figuring out sapply (and similar functions) with multiple parameter user defined function

Hi

So first step is over. Anyway, is there any problem with using dput as I suggested?

Instead of using your date I need to generate my own.

A<-sample(0:2, 10, replace=T)
B<-sample(0:2, 10, replace=T)
C<-sample(0:2, 10, replace=T)
df<-data.frame(A,B,C)

df[df[,2]==2,2]<-3
df$C<-as.numeric(as.character(factor(df$C, labels=c(0,2,3))))

df
   A B C
1  0 3 3
2  0 1 2
3  0 3 2
4  1 0 3
5  1 0 3
6  2 3 2
7  1 3 2
8  2 3 3
9  1 1 0
10 0 0 3
Instead of those tricky ifs (uff uff) you can use either of these

df[df[,2]==2,2]<-3
df$C<-as.numeric(as.character(factor(df$C, labels=c(0,2,3))))

df
   A B C
1  0 3 3
2  0 1 2
3  0 3 2
4  1 0 3
5  1 0 3
6  2 3 2
7  1 3 2
8  2 3 3
9  1 1 0
10 0 0 3

And here I am lost again.

Please, can you clearly state the way how do you want to choose preferences based on values in those three columns.

Regards
Petr