我的数据如下:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/chat_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lorem ipsum"
app:layout_constrainedWidth="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_default="wrap" />
<TextView
android:id="@+id/tvRate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/half_margin"
android:background="@drawable/tag_marketplace_backround"
android:paddingStart="@dimen/half_margin"
android:paddingEnd="@dimen/half_margin"
android:text="@string/market"
android:textAllCaps="true"
android:textColor="@android:color/white"
android:textSize="12sp"
app:layout_constraintStart_toEndOf="@id/chat_message"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
(实际上,多年来有更多的列,其中有些是Y行中的非NA。但这将解决我的问题。)
我要进行操作...
library(tidyverse)
df <- tibble(
Type = c(rep("A", 2), rep("B", 2), rep("A", 2), rep("B", 2)),
Source = c(rep("X", 4), rep("Y", 4)),
ID = c(1001:1008),
January = c(11, 22, 10, 30, NA, NA, NA, NA),
February = c(10, 42, 15, 27, NA, NA, NA, NA)
)
...但是我收到错误newDF <- df %>%
group_by(Type, Source) %>%
summarize(theTotal = sum(January:February, na.rm = TRUE))
。我知道为什么会收到此错误:在某些行中,一月和二月是NA。即使一月仍然是NA,即使二月在这些行中都有数字,我也会收到此错误。
我的问题是:1)为什么Error in January:February : NA/NaN argument
不足以阻止这种情况的发生? 2)我可以对我的代码执行什么操作,以确保对于A / B和Y的那些组合我得到0?
答案 0 :(得分:1)
在这种情况下,可能可以使用summarise_at
,然后使用sum
创建单个列。在grouping_by
感兴趣的列之后,我们得到了。列“一月”至“二月”中的sum
作为单行。 summarise_at
,然后ungroup
,然后再次获得sum
library(dplyr)
df %>%
group_by(Type, Source) %>%
summarise_at(vars(January:February), sum, na.rm = TRUE) %>%
ungroup %>%
transmute(Type, Source,
theTotal = rowSums(select(.,January:February), na.rm = TRUE))
# A tibble: 4 x 3
# Type Source theTotal
# <chr> <chr> <dbl>
#1 A X 85
#2 A Y 0
#3 B X 82
#4 B Y 0
或者另一个选择是
library(purrr)
df %>%
group_split(Type, Source) %>%
map_dfr(~ .x %>%
summarise(Type = first(Type), Source = first(Source),
theTotal = select(., January:February) %>% unlist %>% sum(., na.rm = TRUE)))
# A tibble: 4 x 3
# Type Source theTotal
# <chr> <chr> <dbl>
#1 A X 85
#2 A Y 0
#3 B X 82
#4 B Y 0