gpt4 book ai didi

R/dplyr : Transforming two rows into two columns

转载 作者:行者123 更新时间:2023-12-02 20:25:37 27 4
gpt4 key购买 nike

我在 R 中有一个数据框,如下所示:

Word    Base    Number  Type
- - - -
shoe shoe 4834 singular
shoes shoe 49955 plural
toy toy 75465 singular
toys toy 23556 plural
key key 39485 singular
keys key 6546 plural
jazz jazz 58765 plural

我想将其转换为如下所示:

Word_Sg Word_Pl Base    Num_Singular    Num_Plural
-- -- -- -- --
shoe shoes shoe 4834 49955
toy toys toy 75465 23556
key keys key 39485 6546
NA jazz jazz NA 58765

因此,我不想使用两行来存储单数和复数的值,而是想要两列,一列包含单数数字,一列包含复数数字。

我已经使用 dplyr::summarize 尝试了一些操作,但到目前为止,没有任何成功。这是我到目前为止想出的代码:

dataframe1 <- dataframe %>% 
mutate(Num_Singular = case_when(Type == "singular" ~ Number)) %>%
mutate(Num_Plural = case_when(Type == "plural" ~ Number)) %>%
dplyr::select(Word, Base, Num_Singular, Num_Plural) %>%
group_by(Base) %>%
dplyr::summarize(Num_Singular = paste(na.omit(Num_Singular)),
Num_Plural = paste(na.omit(Num_Plural))

但是,它给了我这个错误:

Error in summarise_impl(.data, dots) : 
Column `Num_Singular` must be length 1 (a summary value), not 2)

我认为问题可能在于有些行不一定具有单数和复数,而只有其中之一(例如“jazz”)。不过大多数行都有。

那么我如何在 R 或 dplyr 中执行此操作?

最佳答案

如果您首先查看前几列::

select(dat, Base, Word, Type)[1:2,]
# Base Word Type
# 1 shoe shoe singular
# 2 shoe shoes plural

从这里开始,考虑它只是将其扩展为单数/复数列,有效地从“高”到“宽”。 (如果 Type 中有两个以上类别,情况会更加明显。)

select(dat, Base, Word, Type) %>%
spread(Type, Word) %>%
rename(Word_Pl=plural, Word_Sg=singular)
# Base Word_Pl Word_Sg
# 1 jazz jazz <NA>
# 2 key keys key
# 3 shoe shoes shoe
# 4 toy toys toy

您也可以轻松地对 Number 重复此操作。从那里开始,只需根据键列 Base 合并/连接它们即可:

full_join(
select(dat, Base, Word, Type) %>%
spread(Type, Word) %>%
rename(Word_Pl=plural, Word_Sg=singular),
select(dat, Base, Number, Type) %>%
spread(Type, Number) %>%
rename(Num_Pl=plural, Num_Sg=singular),
by = "Base"
)
# Base Word_Pl Word_Sg Num_Pl Num_Sg
# 1 jazz jazz <NA> 58765 NA
# 2 key keys key 6546 39485
# 3 shoe shoes shoe 49955 4834
# 4 toy toys toy 23556 75465

消耗数据:

library(dplyr)
library(tidyr)
dat <- read.table(text='Word Base Number Type
shoe shoe 4834 singular
shoes shoe 49955 plural
toy toy 75465 singular
toys toy 23556 plural
key key 39485 singular
keys key 6546 plural
jazz jazz 58765 plural', header=TRUE, stringsAsFactors=FALSE)

关于R/dplyr : Transforming two rows into two columns,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50058198/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com