将数据集拆分为嵌套的数据帧列表,然后使用Tidyr和Purrr进行传播

时间:2017-08-14 01:27:15

标签: r tidyverse purrr

library(ggmosaic)
library(tidyverse)

以下是示例代码

happy2<-happy%>%
select(sex,marital,degree,health)%>%
group_by(sex,marital,degree,health)%>%
summarise(Count=n())

以下代码将数据集拆分为嵌套列表,其中包含度量变量的每个类别的男性和女性表(性别变量)。

happy2 %>% 
split(.$degree) %>% 
lapply(function(x) split(x, x$sex))

这是我现在正在努力的地方。我想重塑,或使用Tidyr,传播“婚姻”变量,或者这应该再次拆分,以便每个“婚姻”类别是一个列标题,每列包含“健康”变量和相应的“计数” ”。可以删除冗余的“性别”和“度”列。

由于我正在使用列表,我一直在尝试使用Tidyverse方法,例如,我一直在尝试使用purrr来删除变量:

happy2%>%map(~select(.x,-sex)

我在想我也可以使用purrr进行传播,但是我无法完成这项工作。

为了帮助说明我正在寻找的东西,我附上了可能结构的图片。我没有包括所有类别,并且计数不正确,因为我只显示结构。我认为“婚姻”类别也可以是第三个拆分变量,如果这更容易吗?因此,我希望的是每个学位类别的男性和女性表格,以及健康婚姻并显示相应的计数。

帮助将不胜感激......

enter image description here

2 个答案:

答案 0 :(得分:0)

以下工作会怎样?我按性别更改了split的语法,以便我可以将后续命令链接在一起:

happy2 %>% 
  split(.$degree) %>% 
  lapply(function(x) x %>% split(.$sex) %>%
           lapply(function(x) x %>% select(-sex, -degree) %>%
                    spread(health, Count)))

修改

这将为您提供每个婚姻状况的单独表格:

happy2 %>% 
  ungroup() %>%
  split(.$degree) %>% 
  lapply(function(x) x %>% split(.$sex) %>%
           lapply(function(x) x %>% select(-sex, -degree) %>% split(.$marital)))

如果您不希望第一列显示婚姻状况,则以下版本会删除:

happy2 %>% 
  ungroup() %>%
  split(.$degree) %>% 
  lapply(function(x) x %>% split(.$sex) %>%
           lapply(function(x) x %>% select(-sex, -degree) %>% split(.$marital) %>%
                    lapply(function(x) x %>% select(-marital))))

答案 1 :(得分:0)

这个怎么样:

# cleaned up your code a bit
# removed the select (as it does nothing)
# consistent column names (count is lower case like the rest of the variables)
# added spacing
happy2 <- happy %>%
  group_by(sex, marital, degree, health) %>%
  summarise(count=n())

happy2 %>%
  dplyr::ungroup() %>% 
  split(list(.$degree, .$sex, .$marital)) %>% 
  lapply(. %>% select(health, count))

或者您真的希望"martial"列中"health"列的<ViewCell> <ViewCell.View> <StackLayout Spacing="0" Padding="0" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"> <StackLayout Orientation="Horizontal" Spacing="10" Padding="0" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"> <StackLayout Orientation="Horizontal" HorizontalOptions="StartAndExpand"> <controls:CircleImage Style="{StaticResource profileImageStyle}" Margin="10, 10, 10, 10" Source="{Binding Source}" BorderColor="White" BorderThickness="2" VerticalOptions="Center" HorizontalOptions="Center"> <controls:CircleImage.GestureRecognizers> <TapGestureRecognizer Tapped="OnChildDetailTapped" /> </controls:CircleImage.GestureRecognizers> </controls:CircleImage> <StackLayout VerticalOptions="Fill" Spacing="1" Padding="0,20,0,10" HorizontalOptions="StartAndExpand"> <StackLayout.GestureRecognizers> <TapGestureRecognizer Tapped="OnChildDetailTapped" /> </StackLayout.GestureRecognizers> <StackLayout Orientation="Vertical" VerticalOptions="Center" HorizontalOptions="StartAndExpand"> <Label x:Name="childName" Text="{Binding DisplayName}" Style="{StaticResource normalFont}"> </Label> <local:ChildInfoIconsView Child="{Binding .}" VerticalOptions="Fill"> </local:ChildInfoIconsView> <Label x:Name="childNotes" Style="{StaticResource footnoteFont}" Text="{Binding ChildNotes, StringFormat={x:Static local:AppResources.formatNotes}}" IsVisible="{Binding HasChildNotes}"> </Label> <Label x:Name="noPickupReason" Style="{StaticResource footnoteFont}" Text="{Binding NoPickupReason, StringFormat={x:Static local:AppResources.formatNoPickupReason}}" IsVisible="{Binding HasNoPickupReason}"> </Label> <Label x:Name="absentReason" Style="{StaticResource footnoteFont}" Text="{Binding AbsentReason, StringFormat={x:Static local:AppResources.formatAbsentReason}}" IsVisible="{Binding HasAbsentReason}"> </Label> </StackLayout> </StackLayout> </StackLayout> <StackLayout Orientation="Horizontal" VerticalOptions="FillAndExpand" HorizontalOptions="End"> <StackLayout.Padding> <OnIdiom x:TypeArguments="Thickness"> <OnIdiom.Phone>5, 20, 10, 20</OnIdiom.Phone> <OnIdiom.Tablet>10, 30, 30, 30</OnIdiom.Tablet> </OnIdiom> </StackLayout.Padding> <Image Style="{StaticResource listviewButtonStyle}" IsVisible="{Binding EnabledSigning, Source={x:Reference page}}" Source="ic_action_yes.png" VerticalOptions="FillAndExpand" HorizontalOptions="End"> <Image.GestureRecognizers> <TapGestureRecognizer Tapped="OnAttend" /> </Image.GestureRecognizers> <Image.Margin> <OnIdiom x:TypeArguments="Thickness"> <OnIdiom.Phone>0, 0, 5, 0</OnIdiom.Phone> <OnIdiom.Tablet>5, 5, 20, 5</OnIdiom.Tablet> </OnIdiom> </Image.Margin> </Image> <Image Style="{StaticResource listviewButtonStyle}" IsVisible="{Binding EnabledSigning, Source={x:Reference page}}" Source="ic_action_no.png" VerticalOptions="FillAndExpand" HorizontalOptions="End"> <Image.GestureRecognizers> <TapGestureRecognizer Tapped="OnAbsent" /> </Image.GestureRecognizers> <Image.Margin> <OnIdiom x:TypeArguments="Thickness"> <OnIdiom.Phone>5, 0, 0, 0</OnIdiom.Phone> <OnIdiom.Tablet>20, 5, 5, 5</OnIdiom.Tablet> </OnIdiom> </Image.Margin> </Image> </StackLayout> </StackLayout> </StackLayout> </ViewCell.View> </ViewCell> 状态位于您的照片中吗?