R 将计数数据转换为多个二进制记录
我有计数数据(Pup列),需要将其转换为二进制记录。我想根据计数数据创建二进制记录,例如,如果我的计数为7,我想创建7个二进制(状态)记录,同时重复并保留所有相关数据 我有几个数据集,我需要这样做,所以我想创建一个函数来做到这一点R 将计数数据转换为多个二进制记录,r,R,我有计数数据(Pup列),需要将其转换为二进制记录。我想根据计数数据创建二进制记录,例如,如果我的计数为7,我想创建7个二进制(状态)记录,同时重复并保留所有相关数据 我有几个数据集,我需要这样做,所以我想创建一个函数来做到这一点 Pup(count) Year Survey.Typ File.Name ice.ras.va 2 2008 Air asi-n3125-20080223.tif 200 -32
Pup(count) Year Survey.Typ File.Name ice.ras.va
2 2008 Air asi-n3125-20080223.tif 200 -32.0000
我希望在Pups列上有显示存在和不存在的数据框,以便在一个站点有多个计数时,将有多个存在数据记录
预期结果是:
Pup(present) Year Survey.Typ File.Name ice.ras.va
1 2008 Air asi-n3125-20080223.tif 200 -32.0000
1 2008 Air asi-n3125-20080223.tif 200 -32.0000
一种解决方案是在单元格中创建一个“存在”事件列表,然后使用
tidyr
包中的unest
取消列表,即展开data.frame
library("dplyr")
# Making up some data
df <- data.frame(pup_id = 1:10,
metadata = sample(LETTERS, 10),
count = rpois(10, 3) + 1L)
# pup_id metadata count
# 1 1 M 2
# 2 2 X 6
# 3 3 O 3
# 4 4 Z 6
# 5 5 H 7
# 6 6 U 1
# 7 7 L 2
# 8 8 P 5
# 9 9 Q 6
# 10 10 S 8
# Create a vector of presence for each pup
df_with_presence_list <-
df %>%
group_by(pup_id) %>%
mutate(presence = list(rep(1, count)))
# This has added a list-column that we can now unnest
# You can do it all in one step but it was to show you
df_with_presence <-
df_with_presence_list %>%
tidyr::unnest(presence)
# # A tibble: 46 x 4
# # Groups: pup_id [10]
# pup_id metadata count presence
# <int> <fct> <int> <dbl>
# 1 1 M 2 1
# 2 1 M 2 1
# 3 2 X 6 1
# 4 2 X 6 1
# 5 2 X 6 1
# 6 2 X 6 1
# 7 2 X 6 1
# 8 2 X 6 1
# 9 3 O 3 1
# 10 3 O 3 1
# # … with 36 more rows
库(“dplyr”)
#编造一些数据
df%
变异(存在=列表(代表(1,计数)))
#这增加了一个列表列,我们现在可以取消它
#你可以在一个步骤中完成所有的事情,但这是为了向你展示
df_与_存在百分比
tidyr::unnest(存在)
##A tibble:46 x 4
##组:幼犬id[10]
#存在pup_id元数据计数
#
#1米2 1
#2 1米2 1
#3 2 X 6 1
#4 2 X 6 1
#5 2 X 6 1
#62x61
#72x61
#82x61
#93O31
#103O31
##…还有36行
一种解决方案是在单元格中创建“存在”事件列表,然后使用tidyr
包中的unnest
取消列表,即展开data.frame
library("dplyr")
# Making up some data
df <- data.frame(pup_id = 1:10,
metadata = sample(LETTERS, 10),
count = rpois(10, 3) + 1L)
# pup_id metadata count
# 1 1 M 2
# 2 2 X 6
# 3 3 O 3
# 4 4 Z 6
# 5 5 H 7
# 6 6 U 1
# 7 7 L 2
# 8 8 P 5
# 9 9 Q 6
# 10 10 S 8
# Create a vector of presence for each pup
df_with_presence_list <-
df %>%
group_by(pup_id) %>%
mutate(presence = list(rep(1, count)))
# This has added a list-column that we can now unnest
# You can do it all in one step but it was to show you
df_with_presence <-
df_with_presence_list %>%
tidyr::unnest(presence)
# # A tibble: 46 x 4
# # Groups: pup_id [10]
# pup_id metadata count presence
# <int> <fct> <int> <dbl>
# 1 1 M 2 1
# 2 1 M 2 1
# 3 2 X 6 1
# 4 2 X 6 1
# 5 2 X 6 1
# 6 2 X 6 1
# 7 2 X 6 1
# 8 2 X 6 1
# 9 3 O 3 1
# 10 3 O 3 1
# # … with 36 more rows
库(“dplyr”)
#编造一些数据
df%
变异(存在=列表(代表(1,计数)))
#这增加了一个列表列,我们现在可以取消它
#你可以在一个步骤中完成所有的事情,但这是为了向你展示
df_与_存在百分比
tidyr::unnest(存在)
##A tibble:46 x 4
##组:幼犬id[10]
#存在pup_id元数据计数
#
#1米2 1
#2 1米2 1
#3 2 X 6 1
#4 2 X 6 1
#5 2 X 6 1
#62x61
#72x61
#82x61
#93O31
#103O31
##…还有36行
您能举一个您想要的输出的例子吗?为了更清楚地了解您希望获得的内容,您是否可以提供一份数据样本,例如使用dput(head(df))
您是否可以展示一个您想要的输出示例?为了更清楚地了解您希望获得的内容,您还可以提供一份数据样本,例如使用dput(head(df))