'how to apply a function to single data frame column
I have a set of models. Each model has has a column cluster which is gives each row a cluster as a categorical variable (with some amount of numbers ranging from 1-120). There is also a column p.val with nums ranging from 0-1. I want to print the row with the highest p.val value for each of the unique cluster values.
The following code does what I want for a single model:
for (j in unique(model$cluster))
{
print(j)
print(max(model[model$cluster==j,]$p.val))
}
[1] 2
[1] 0.491592
[1] 3
[1] 0.8446473
[1] 4
[1] 0.49965
[1] 1
[1] 0.7386532
How do I write this as a function that allows me to do:
func(mod2)
I am unfamiliar with writing functions in R and I am running into issues re: when to use apply.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
