so I am trying to find the number of samples that are labelled with the phrase 'PaperB' in a very larger data frame in R, I have used the following:
df[grep("PaperB", names(df))]
but this has given me all the columns labelled with Tissue B and their values, rather than the total amount of PaperB samples, how could i change this to just get the total amount?
thanks for any help
CodePudding user response:
strong textIf I understood you correctly:
Here is dplyr
solution using the iris
dataset:
Here we count the columns that contain the string "Sepal" in their name with length()
function:
library(dplyr)
iris %>%
select(contains("Sepal")) %>%
length()
[1] 2
BASE R For you code we would do:
length(iris[grep("Sepal", names(iris))])
CodePudding user response:
As the data frame is large I would avoid pulling its data and deal only with metadata. You were very close:
cn <- colnames(iris)
gr <- grep("PaperB", cn)
length(gr)
Or in a pipeline:
cn <- colnames(iris)
grep("PaperB", cn) |>
length()