sum - countif within R repeated across each row -
i'm having trouble trying replicate of countif function i'm familiar in excel. i've got data frame, , has large number of rows. i'm trying take 2 variables (x & z) , countif of how many other variables within dataframe match that. figured out doing:
sum('mydataframe'$x==`mydataframe`$x[1]&`mydataframe'$z==`mydataframe`$z[1])
this gives me correct countif x&z within whole data set first row [1]. problem i've got use [1]. i've tried using (with,...) command, can no longer access whole column.
i'd able count of x & z combination each row within data frame have output new vector can add column. , i'd go on every row through end.
hopefully pretty simple. figure combination of (with,..) or apply or it, i'm new.
i interested in count total in every instance, not running sequential count.
it seems asking way create new column contains number of rows in entire data frame x
, z
value equal values of variables row.
with bit of sample data:
(dat <- data.frame(x=c(1, 1, 2), z=c(3, 3, 3))) # x z # 1 1 3 # 2 1 3 # 3 2 3
one simple approach grouping dplyr
's group_by
function , creating new column number of elements in group:
library(dplyr) dat %>% group_by(x, z) %>% mutate(n=n()) # x z n # (dbl) (dbl) (int) # 1 1 3 2 # 2 1 3 2 # 3 2 3 1
a base r solution involve ave
:
dat$n <- ave(rep(na, nrow(dat)), dat$x, dat$z, fun=length) dat # x z n # 1 1 3 2 # 2 1 3 2 # 3 2 3 1
Comments
Post a Comment