使用 mutate 创建了一个新列,结果是一个充满 NA 的列(但此代码适用于我拥有的不同文件)
Created a new column using mutate, result is a column full of NAs (but this code worked for a different file I have)
我正在使用 tidyr 并使用 mutate 创建一个新列来计算在我拥有的不同列中返回了多少个 0。出于某种原因,虽然新的专栏形式,但我在整个专栏中收到 NA,即使我可以看到至少应该有一个答案(例如,我在一列中看到 0,但是 "count"(总计) 列仍为 N/A".
这段代码之前在几乎相同的数据集上处理过相同类型的问题,有人可以向我解释发生了什么吗?我的代码副本如下。
Gathered <- ScottCrkMeta250918 %>%
gather(SNP, Genotype, 43:234)
Prefailed <- Gathered %>%
group_by(NMFS_DNA_ID, BOX_ID,BOX_POSITION) %>%
mutate(Count = sum(Genotype == 0))
我试图查看有多少 SNP 失败,因此我在失败的列中有 0。我试图告诉 R 计算这些零(失败)并在单独的列中将它们提供给我。
很遗憾,您不共享数据,所以这是一个猜测。所以我 猜测 Genotype
包含 NA
。在这种情况下,请尝试将您的代码替换为
Prefailed <- Gathered %>%
group_by(NMFS_DNA_ID, BOX_ID, BOX_POSITION) %>%
mutate(Count = sum(Genotype == 0, na.rm = TRUE))
这是一个最小的可重现代码示例来演示
set.seed(2018)
df <- data.frame(
Genotype = sample(c(NA, 0, 1), 10, replace = T))
df %>%
mutate(
Count_without_NA_removed = sum(Genotype == 0),
Count_with_NA_removed = sum(Genotype == 0, na.rm = T))
# Genotype Count_without_NA_removed Count_with_NA_removed
#1 0 NA 5
#2 0 NA 5
#3 NA NA 5
#4 NA NA 5
#5 0 NA 5
#6 NA NA 5
#7 0 NA 5
#8 NA NA 5
#9 1 NA 5
#10 0 NA 5
我正在使用 tidyr 并使用 mutate 创建一个新列来计算在我拥有的不同列中返回了多少个 0。出于某种原因,虽然新的专栏形式,但我在整个专栏中收到 NA,即使我可以看到至少应该有一个答案(例如,我在一列中看到 0,但是 "count"(总计) 列仍为 N/A".
这段代码之前在几乎相同的数据集上处理过相同类型的问题,有人可以向我解释发生了什么吗?我的代码副本如下。
Gathered <- ScottCrkMeta250918 %>%
gather(SNP, Genotype, 43:234)
Prefailed <- Gathered %>%
group_by(NMFS_DNA_ID, BOX_ID,BOX_POSITION) %>%
mutate(Count = sum(Genotype == 0))
我试图查看有多少 SNP 失败,因此我在失败的列中有 0。我试图告诉 R 计算这些零(失败)并在单独的列中将它们提供给我。
很遗憾,您不共享数据,所以这是一个猜测。所以我 猜测 Genotype
包含 NA
。在这种情况下,请尝试将您的代码替换为
Prefailed <- Gathered %>%
group_by(NMFS_DNA_ID, BOX_ID, BOX_POSITION) %>%
mutate(Count = sum(Genotype == 0, na.rm = TRUE))
这是一个最小的可重现代码示例来演示
set.seed(2018)
df <- data.frame(
Genotype = sample(c(NA, 0, 1), 10, replace = T))
df %>%
mutate(
Count_without_NA_removed = sum(Genotype == 0),
Count_with_NA_removed = sum(Genotype == 0, na.rm = T))
# Genotype Count_without_NA_removed Count_with_NA_removed
#1 0 NA 5
#2 0 NA 5
#3 NA NA 5
#4 NA NA 5
#5 0 NA 5
#6 NA NA 5
#7 0 NA 5
#8 NA NA 5
#9 1 NA 5
#10 0 NA 5