r: find lowest value matching criteria over columns -
my data frame looks this
personid t1 t2 t3 1 0 11 0 1 0 11 0 2 0 11 13 2 0 11 13 3 0 0 0 3 0 0 0
i need make sure each person has 1 test score above 10. if not, have removed data frame. want keep track of lowest score above 10, , add new column.
thus, result this:
personid t1 t2 t3 new 1 0 11 0 11 1 0 11 0 11 2 0 11 13 11 2 0 11 13 11
if go data.table
route, think melt , join:
library(data.table) setdt(dat) dat[ melt(dat, id.vars="personid")[value > 10, .(new=min(value)), by=personid], on="personid" ] # personid t1 t2 t3 new #1: 1 0 11 0 11 #2: 1 0 11 0 11 #3: 2 0 11 13 11 #4: 2 0 11 13 11
Comments
Post a Comment