View Full Version : Problem with R-code


Random
01-17-2008, 12:26 PM
Dear all,
I've got a problem with an r-code: I've got a list of frequency tables; below you can see it, it's named test.
Now I want to create a new variable (a vector or a list) which contains for every element of the list the category with the maximum count. SO for the first element until the fifth element of the list the new variable should have the value 1, and for the 6th and 7th element the value 0.
Can anyone help me please.
Thanks in advance

Kathy


> test<-apply(predictMajV,1,table)
> test
[[1]]

0 1
2 498

[[2]]

0 1
2 498

[[3]]

0 1
14 486

[[4]]

0 1
5 495

[[5]]

0 1
9 491

[[6]]

0 1
497 3

[[7]]

0
500

Mike White
01-17-2008, 04:37 PM
You can use lapply, which.max and name functions to get the name of the category with the maximum value as in the attached example. If both categories have the same frequency which.max gets the first one, so you may want to check for equal frequencies as in the attached code. This also shows if there was only one category.