How to calculate row counts in dataframe of specific cells in R -
i asked similar question before here: how calculate percentage of cells in data frame start sequence in r?
i'll copy of on ask new question.
i have data looks like:
set_1 set_2 set_3 set_4 set_5 set_6 set_7 abc89 abc62 67 abc513 abc512 abc81 abc10 abc6 pop abc11 abc4 giant 13 abc15 abc90 abc16 abc123 abc33 abc22 abc08 9 111 abc15 abc72 abc36 abc57 abc9 abc55
i make histogram of row counts. count cells start "abc". row 1 have 6 cells. row 2 has 4 cells. row 3 has 6 cells, etc. make histogram of this. how can done in r? data in data.frame.
you can count number of entries starting "abc" in each row with
y <- apply(df, 1, function(x) sum(grepl("^abc", x))) #> y #[1] 6 4 6 6
this result plotted in histogram with
hist(y, breaks=c(1:max(y)), main = "frequency of 'abc' entries", col="lightblue")
if prefer graphical representation of value of "abc" counts each row, use barplot()
instead of hist()
:
barplot(y, main = "number of 'abc' entries in each row",col="lightblue")
data
text <- "set_1 set_2 set_3 set_4 set_5 set_6 set_7 abc89 abc62 67 abc513 abc512 abc81 abc10 abc6 pop abc11 abc4 giant 13 abc15 abc90 abc16 abc123 abc33 abc22 abc08 9 111 abc15 abc72 abc36 abc57 abc9 abc55" df <- read.table(text=text, header=t)
Comments
Post a Comment