其他分享
首页 > 其他分享> > R语言中统计数据框中指定字符出现的次数

R语言中统计数据框中指定字符出现的次数

作者:互联网

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)

 

标签:字符,统计数据,指定,dat,次数,框中
来源: https://www.cnblogs.com/liujiaxin2018/p/15690346.html