根据 B 列中的日期和 R 中 C 列中的指定非匹配值在 A 列中添加元素
Add elements in column A based on dates in column B and specified non-matching values in column C in R
我 运行 一个狩猎程序,并有一个包含以下列的数据框:日期、物种类型、努力程度,以及代表该日期在特定狩猎区域收获的物种数量的几列。但是,"species type" 列将同一物种的雄性、雌性和幼体分开。我需要折叠每个区域相同物种的收获数量,同时保留所有其他公共信息。这是我的 df 的示例:
Date Species Area.1.Harvest Area.2.Harvest Effort
2016-04-02 Wild Sheep-M 1 NA 30
2016-04-02 Wild Sheep-F 4 NA 30
2016-04-17 Feral Goat-M NA 5 50
2016-04-17 Feral Goat-F NA 3 50
2016-09-18 Wild Sheep-M NA 6 60
2016-09-18 Wild Sheep-F NA 1 60
2016-09-18 Wild Sheep-J NA 1 60
这是我要查找的结果:
Date Species Area.1.Harvest Area.2.Harvest Effort
2016-04-02 Wild Sheep 5 NA 30
2016-04-17 Feral Goat NA 8 50
2016-09-18 Wild Sheep NA 8 60
我有 6 个不同的领域可以做这件事,并且有 3 年的收获数据。
查看库 dplyr,其中函数 group_by()
和 summarise()
对您正在寻找的聚合类型非常有帮助。
查看库 stringr,其中 str_sub()
等函数可以帮助您管理和转换字符串(在这种情况下,列 Species 应该 字符而不是因子).
library(dplyr)
library(stringr)
df %>%
mutate(
Species = str_sub(Species, 1, nchar(Species) - 2)
) %>%
group_by(Date, Species) %>%
summarise(
Area.1.Harvest = sum(Area.1.Harvest, na.rm = T),
Area.2.Harvest = sum(Area.2.Harvest, na.rm = T),
Effort = mean(Effort, na.rm = T)
)
您也可以使用 data.table 库
轻松完成此操作
library(data.table)
df <- data.table(Date = as.Date(c(rep('2016-04-02',2), rep('2016-04-17',2), rep('2016-09-18',3))), Species = c('Wild Sheep-M', 'Wild Sheep-F', 'Feral Goat-M', 'Feral Goat-F', 'Wild Sheep-M', 'Wild Sheep-F','Wild Sheep-J'), Area.1.Harvest = c(1,4,NA,NA,NA,NA,NA), Area.2.Harvest = c(NA,NA,5,3,6,1,1), Effort = c(30, 30, 50, 50, 60, 60, 60))
df[,Species := substr(Species,1,nchar(Species)-2)][,.(Area.1.Harvest = sum(Area.1.Harvest, na.rm=TRUE),
Area.2.Harvest = sum(Area.2.Harvest, na.rm=TRUE),
Effort = mean(Effort, na.rm=TRUE)), by=list(Date, Species)]
# Date Species Area.1.Harvest Area.2.Harvest Effort
#1: 2016-04-02 Wild Sheep 5 0 30
#2: 2016-04-17 Feral Goat 0 8 50
#3: 2016-09-18 Wild Sheep 0 8 60
您可以仅使用 dplyr
执行以下操作:
library(dplyr)
df %>%
group_by(Species = gsub("-.*", "", Species), Date) %>%
mutate_at(vars(contains("Area")), function(x) sum(x, na.rm = any(!is.na(x)))) %>%
mutate_at(vars(contains("Effort")), function(x) mean(x, na.rm = any(!is.na(x)))) %>%
distinct()
无论您有多少 Area
或 Effort
变量,这都有效(因为您提到您有多个变量,而您的示例只是部分表示)。
输出:
# A tibble: 3 x 5
# Groups: Species, Date [3]
Date Species Area.1.Harvest Area.2.Harvest Effort
<chr> <chr> <int> <int> <dbl>
1 2016-04-02 WildSheep 5 NA 30
2 2016-04-17 FeralGoat NA 8 50
3 2016-09-18 WildSheep NA 8 60
自定义函数用于 mean
和 sum
,如往常一样mean(x, na.rm = T)
将 return 0 而不是所需输出中指定的 NA
。
我 运行 一个狩猎程序,并有一个包含以下列的数据框:日期、物种类型、努力程度,以及代表该日期在特定狩猎区域收获的物种数量的几列。但是,"species type" 列将同一物种的雄性、雌性和幼体分开。我需要折叠每个区域相同物种的收获数量,同时保留所有其他公共信息。这是我的 df 的示例:
Date Species Area.1.Harvest Area.2.Harvest Effort
2016-04-02 Wild Sheep-M 1 NA 30
2016-04-02 Wild Sheep-F 4 NA 30
2016-04-17 Feral Goat-M NA 5 50
2016-04-17 Feral Goat-F NA 3 50
2016-09-18 Wild Sheep-M NA 6 60
2016-09-18 Wild Sheep-F NA 1 60
2016-09-18 Wild Sheep-J NA 1 60
这是我要查找的结果:
Date Species Area.1.Harvest Area.2.Harvest Effort
2016-04-02 Wild Sheep 5 NA 30
2016-04-17 Feral Goat NA 8 50
2016-09-18 Wild Sheep NA 8 60
我有 6 个不同的领域可以做这件事,并且有 3 年的收获数据。
查看库 dplyr,其中函数 group_by()
和 summarise()
对您正在寻找的聚合类型非常有帮助。
查看库 stringr,其中 str_sub()
等函数可以帮助您管理和转换字符串(在这种情况下,列 Species 应该 字符而不是因子).
library(dplyr)
library(stringr)
df %>%
mutate(
Species = str_sub(Species, 1, nchar(Species) - 2)
) %>%
group_by(Date, Species) %>%
summarise(
Area.1.Harvest = sum(Area.1.Harvest, na.rm = T),
Area.2.Harvest = sum(Area.2.Harvest, na.rm = T),
Effort = mean(Effort, na.rm = T)
)
您也可以使用 data.table 库
轻松完成此操作library(data.table)
df <- data.table(Date = as.Date(c(rep('2016-04-02',2), rep('2016-04-17',2), rep('2016-09-18',3))), Species = c('Wild Sheep-M', 'Wild Sheep-F', 'Feral Goat-M', 'Feral Goat-F', 'Wild Sheep-M', 'Wild Sheep-F','Wild Sheep-J'), Area.1.Harvest = c(1,4,NA,NA,NA,NA,NA), Area.2.Harvest = c(NA,NA,5,3,6,1,1), Effort = c(30, 30, 50, 50, 60, 60, 60))
df[,Species := substr(Species,1,nchar(Species)-2)][,.(Area.1.Harvest = sum(Area.1.Harvest, na.rm=TRUE),
Area.2.Harvest = sum(Area.2.Harvest, na.rm=TRUE),
Effort = mean(Effort, na.rm=TRUE)), by=list(Date, Species)]
# Date Species Area.1.Harvest Area.2.Harvest Effort
#1: 2016-04-02 Wild Sheep 5 0 30
#2: 2016-04-17 Feral Goat 0 8 50
#3: 2016-09-18 Wild Sheep 0 8 60
您可以仅使用 dplyr
执行以下操作:
library(dplyr)
df %>%
group_by(Species = gsub("-.*", "", Species), Date) %>%
mutate_at(vars(contains("Area")), function(x) sum(x, na.rm = any(!is.na(x)))) %>%
mutate_at(vars(contains("Effort")), function(x) mean(x, na.rm = any(!is.na(x)))) %>%
distinct()
无论您有多少 Area
或 Effort
变量,这都有效(因为您提到您有多个变量,而您的示例只是部分表示)。
输出:
# A tibble: 3 x 5
# Groups: Species, Date [3]
Date Species Area.1.Harvest Area.2.Harvest Effort
<chr> <chr> <int> <int> <dbl>
1 2016-04-02 WildSheep 5 NA 30
2 2016-04-17 FeralGoat NA 8 50
3 2016-09-18 WildSheep NA 8 60
自定义函数用于 mean
和 sum
,如往常一样mean(x, na.rm = T)
将 return 0 而不是所需输出中指定的 NA
。