R语言中统计数据框中指定字符出现的次数
1、利用unlist + sum实现
dat <- read.table("a.txt", header = F) ## 统计a.txt中e出现的次数
dat
dat2 <- unlist(dat)
sum(dat2 == "e")
2、利用for循环遍历实现
dat <- read.table("a.txt", header = F)
dat
count = 0
for (i in 1:nrow(dat)) { ## 利用for循环遍历实现统计e的数目
for (j in 1:ncol(dat)) {
if(dat[i,j] == "e"){
count = count + 1
}
}
}
print(count)