I have the following data frame:
Test <- data.frame(Species = c("A","B","C","D"),
WB1=c(0.1,1.1,0.9,1.2),
WB2=c(1, 0.8, 1.3, 1),
WB3=c(0.5, 0.7, 1.2, 0.9),
WB4=c(1.3, 1.2, 0.9, 0.6))
And I would like to get a new data frame per species that only lists the WB's that are lager than one. So in this example for species A that would be
WB1 WB4
1.0 1.3
I have tried the following:
AllSpecies <- Test$Species
AllWaterbodies <- colnames(Test)
for(species in AllSpecies)
{
ind <- which(Test$Species == species)
x <- Test[ind,]
colnames(x) <- AllWaterbodies
If say species <- "A", than this would already give me:
Species WB1 WB2 WB3 NA
1 A 0.1 1 0.5 1.3
now I would like to only list the WB's that are larger than one,
and this is where I am stuck.
Can any body help me to complete my loop?
An R base solution using
lapply:The same result as @Beasterfield’s but no need to install an extra package.
You’re asking for values larger than one, but in your desired output you show values larger or equal to 1, so maybe the code you’re looking for is the following: