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)

