如何计算R中数据集中字符出现的次数
How to count the number of occurances of a character within a data set in R
我目前正在研究一个模拟数据集,我在其中记录了一种蜜蜂访问给定种类的花的次数。我的部分数据集可能如下所示:
Plant Visitor.1 Visitor.2 Visitor.3
1 Bombus Bombus NA
2 Apis Bombus Apis
3 NA NA NA
4 Apis NA NA
5 NA NA NA
6 Apis NA NA
7 Apis Apis Halictid
8 Apis Apis NA
9 Bombus Halictid Halictid
10 NA NA NA
11 NA NA NA
12 NA NA NA
13 Halictid NA NA
有没有办法让我计算出 "Bombus"、"Apis"、"Halictid" 等在每一列中出现了多少次,甚至一次跨越所有三列?我已经阅读了很多有关如何使用数据字符串执行此操作的内容,但没有阅读过诸如此类的数据集。老实说,我不太确定从哪里开始。
你可以试试
library(qdapTools)
addmargins(t(mtabulate(df1[-1])),2)
# Visitor.1 Visitor.2 Visitor.3 Sum
#Apis 5 2 1 8
#Bombus 2 2 0 4
#Halictid 1 1 2 4
如果您只需要整个数据集的计数
table(unlist(df1[-1]))
# Apis Bombus Halictid
# 8 4 4
我不确定你是否需要为每一行,即每一行 'Plant'。那样的话,
Un1 <- unique(na.omit(unlist(df1[-1])))
res <- t(apply(df1[-1], 1, FUN=function(x) table(factor(x, levels=Un1))))
cbind(res, Sum=rowSums(res))
# Bombus Apis Halictid Sum
#[1,] 2 0 0 2
#[2,] 1 2 0 3
#[3,] 0 0 0 0
#[4,] 0 1 0 1
#[5,] 0 0 0 0
#[6,] 0 1 0 1
#[7,] 0 2 1 3
#[8,] 0 2 0 2
#[9,] 1 0 2 3
#[10,] 0 0 0 0
#[11,] 0 0 0 0
#[12,] 0 0 0 0
#[13,] 0 0 1 1
或使用mtabulate
addmargins(as.matrix(mtabulate(as.data.frame(t(df1[-1])))),2)
更新
如果您需要 columns
(仅使用基础 R),
addmargins(t(apply(df1[-1], 2, FUN=function(x) table(factor(x, levels=Un1)))))
# Bombus Apis Halictid Sum
#Visitor.1 2 5 1 8
#Visitor.2 2 2 1 5
#Visitor.3 0 1 2 3
#Sum 4 8 4 16
或者更紧凑的版本是
addmargins(table(stack(df1[-1])[2:1]))
# values
#ind Apis Bombus Halictid Sum
# Visitor.1 5 2 1 8
# Visitor.2 2 2 1 5
# Visitor.3 1 0 2 3
# Sum 8 4 4 16
数据
df1 <- structure(list(Plant = 1:13, Visitor.1 = c("Bombus", "Apis",
NA, "Apis", NA, "Apis", "Apis", "Apis", "Bombus", NA, NA, NA,
"Halictid"), Visitor.2 = c("Bombus", "Bombus", NA, NA, NA, NA,
"Apis", "Apis", "Halictid", NA, NA, NA, NA), Visitor.3 = c(NA,
"Apis", NA, NA, NA, NA, "Halictid", NA, "Halictid", NA, NA, NA,
NA)), .Names = c("Plant", "Visitor.1", "Visitor.2", "Visitor.3"
), class = "data.frame", row.names = c(NA, -13L))
如果您只想要一个简单的解决方案,请尝试将以下代码复制并粘贴到您的 R 控制台中:
## Create an artificial data set.
example.data =
data.frame(col1 = c("Bob", "Jane", "Mary"), col2 = c("Mary", "Joe", "Sam"))
## Count how many times 'x' appears in each column of the data set.
## Lets count how many times 'Bob' appears in each column.
apply(example.data, 2, function(x) length(which(x == 'Bob')))
希望对您有所帮助:)
我目前正在研究一个模拟数据集,我在其中记录了一种蜜蜂访问给定种类的花的次数。我的部分数据集可能如下所示:
Plant Visitor.1 Visitor.2 Visitor.3
1 Bombus Bombus NA
2 Apis Bombus Apis
3 NA NA NA
4 Apis NA NA
5 NA NA NA
6 Apis NA NA
7 Apis Apis Halictid
8 Apis Apis NA
9 Bombus Halictid Halictid
10 NA NA NA
11 NA NA NA
12 NA NA NA
13 Halictid NA NA
有没有办法让我计算出 "Bombus"、"Apis"、"Halictid" 等在每一列中出现了多少次,甚至一次跨越所有三列?我已经阅读了很多有关如何使用数据字符串执行此操作的内容,但没有阅读过诸如此类的数据集。老实说,我不太确定从哪里开始。
你可以试试
library(qdapTools)
addmargins(t(mtabulate(df1[-1])),2)
# Visitor.1 Visitor.2 Visitor.3 Sum
#Apis 5 2 1 8
#Bombus 2 2 0 4
#Halictid 1 1 2 4
如果您只需要整个数据集的计数
table(unlist(df1[-1]))
# Apis Bombus Halictid
# 8 4 4
我不确定你是否需要为每一行,即每一行 'Plant'。那样的话,
Un1 <- unique(na.omit(unlist(df1[-1])))
res <- t(apply(df1[-1], 1, FUN=function(x) table(factor(x, levels=Un1))))
cbind(res, Sum=rowSums(res))
# Bombus Apis Halictid Sum
#[1,] 2 0 0 2
#[2,] 1 2 0 3
#[3,] 0 0 0 0
#[4,] 0 1 0 1
#[5,] 0 0 0 0
#[6,] 0 1 0 1
#[7,] 0 2 1 3
#[8,] 0 2 0 2
#[9,] 1 0 2 3
#[10,] 0 0 0 0
#[11,] 0 0 0 0
#[12,] 0 0 0 0
#[13,] 0 0 1 1
或使用mtabulate
addmargins(as.matrix(mtabulate(as.data.frame(t(df1[-1])))),2)
更新
如果您需要 columns
(仅使用基础 R),
addmargins(t(apply(df1[-1], 2, FUN=function(x) table(factor(x, levels=Un1)))))
# Bombus Apis Halictid Sum
#Visitor.1 2 5 1 8
#Visitor.2 2 2 1 5
#Visitor.3 0 1 2 3
#Sum 4 8 4 16
或者更紧凑的版本是
addmargins(table(stack(df1[-1])[2:1]))
# values
#ind Apis Bombus Halictid Sum
# Visitor.1 5 2 1 8
# Visitor.2 2 2 1 5
# Visitor.3 1 0 2 3
# Sum 8 4 4 16
数据
df1 <- structure(list(Plant = 1:13, Visitor.1 = c("Bombus", "Apis",
NA, "Apis", NA, "Apis", "Apis", "Apis", "Bombus", NA, NA, NA,
"Halictid"), Visitor.2 = c("Bombus", "Bombus", NA, NA, NA, NA,
"Apis", "Apis", "Halictid", NA, NA, NA, NA), Visitor.3 = c(NA,
"Apis", NA, NA, NA, NA, "Halictid", NA, "Halictid", NA, NA, NA,
NA)), .Names = c("Plant", "Visitor.1", "Visitor.2", "Visitor.3"
), class = "data.frame", row.names = c(NA, -13L))
如果您只想要一个简单的解决方案,请尝试将以下代码复制并粘贴到您的 R 控制台中:
## Create an artificial data set.
example.data =
data.frame(col1 = c("Bob", "Jane", "Mary"), col2 = c("Mary", "Joe", "Sam"))
## Count how many times 'x' appears in each column of the data set.
## Lets count how many times 'Bob' appears in each column.
apply(example.data, 2, function(x) length(which(x == 'Bob')))
希望对您有所帮助:)