Распространение фрейма данных с неуникальными значениями в некоторых столбцах

это данные, с которыми я работаю:

> data
         Segment Product               Value           Key
1   non-domestic      S1  517.50760307564053 Actuals Sales
2   non-domestic      S2  1235.3088913918129 Actuals Sales
3   non-domestic      S3  2141.6841816176966 Actuals Sales
4       domestic      S1 -958.38836859580044 Actuals Sales
5       domestic      S2 -1129.5593769492507 Actuals Sales
6       domestic      S3 -137.68477107274975 Actuals Sales
7  non-domestic       S1 -296.07559218703756 Quarter Sales
8  non-domestic       S2  1092.0390648120747 Quarter Sales
9  non-domestic       S3  1156.2866848179935 Quarter Sales
10     domestic       S1 -1975.0222255105061 Quarter Sales
11     domestic       S2 -2549.8125184965966 Quarter Sales
12     domestic       S3 -2608.2434152116011 Quarter Sales

Я пытаюсь разложить его, чтобы получить таблицу с 6 строками и 4 столбцами (Segment, Product, Actuals Sales, Quarter Sales) без пропущенных значений

spread(data=data, key=Key, value=Value)

К сожалению, я получаю вот что. Я понимаю, что это потому, что в столбцах Segment и Product есть неуникальные значения.

         Segment Product       Actuals Sales       Quarter Sales
1       domestic      S1 -958.38836859580044                <NA>
2       domestic      S2 -1129.5593769492507                <NA>
3       domestic      S3 -137.68477107274975                <NA>
4      domestic       S1                <NA> -1975.0222255105061
5      domestic       S2                <NA> -2549.8125184965966
6      domestic       S3                <NA> -2608.2434152116011
7   non-domestic      S1  517.50760307564053                <NA>
8   non-domestic      S2  1235.3088913918129                <NA>
9   non-domestic      S3  2141.6841816176966                <NA>
10 non-domestic       S1                <NA> -296.07559218703756
11 non-domestic       S2                <NA>  1092.0390648120747
12 non-domestic       S3                <NA>  1156.2866848179935

Не могли бы вы помочь мне, как я могу удалить недостающие значения и создать таблицу, в которой значения в первых двух столбцах не дублируются?

Вот воспроизводимый пример:

> dput(data)
structure(list(Segment = c("non-domestic", "non-domestic", "non-domestic", 
"domestic", "domestic", "domestic", "non-domestic ", "non-domestic ", 
"non-domestic ", "domestic ", "domestic ", "domestic "), Product = c("S1", 
"S2", "S3", "S1", "S2", "S3", "S1", "S2", "S3", "S1", "S2", "S3"
), Value = c("517.50760307564053", "1235.3088913918129", "2141.6841816176966", 
"-958.38836859580044", "-1129.5593769492507", "-137.68477107274975", 
"-296.07559218703756", "1092.0390648120747", "1156.2866848179935", 
"-1975.0222255105061", "-2549.8125184965966", "-2608.2434152116011"
), Key = c("Actuals Sales", "Actuals Sales", "Actuals Sales", 
"Actuals Sales", "Actuals Sales", "Actuals Sales", "Quarter Sales", 
"Quarter Sales", "Quarter Sales", "Quarter Sales", "Quarter Sales", 
"Quarter Sales")), .Names = c("Segment", "Product", "Value", 
"Key"), row.names = c(NA, -12L), class = "data.frame")

person Julia    schedule 11.06.2021    source источник
comment
Предоставьте воспроизводимый пример (stackoverflow.com/help/minimal-reproducible-example). Вы можете использовать dput() для создания воспроизводимой и совместно используемой версии вашего фрейма данных.   -  person Pedro Alencar    schedule 11.06.2021
comment
@PedroAlencar готово!   -  person Julia    schedule 11.06.2021
comment
Я думаю, это потому, что у вас есть конечные пробелы в некоторых ваших сегментах. Можете ли вы обрезать их и попробовать еще раз? т.е. data$Segment <- trimws(data$Segment), тогда ваша команда распространения   -  person Hobo    schedule 11.06.2021
comment
@Hobo работал как шарм! Спасибо, не заметил :)   -  person Julia    schedule 11.06.2021
comment
Превосходно; Рад был помочь   -  person Hobo    schedule 11.06.2021


Ответы (4)


Ваш образец данных на самом деле содержит некоторые пробелы, после их удаления pivot_wider вместе со своим аргументом id_cols работает как шарм

data <- structure(list(Segment = c("non-domestic", "non-domestic", "non-domestic", 
                                   "domestic", "domestic", "domestic", "non-domestic", "non-domestic", 
                                   "non-domestic", "domestic", "domestic", "domestic"), Product = c("S1", 
                                                                                                        "S2", "S3", "S1", "S2", "S3", "S1", "S2", "S3", "S1", "S2", "S3"
                                   ), Value = c("517.50760307564053", "1235.3088913918129", "2141.6841816176966", 
                                                "-958.38836859580044", "-1129.5593769492507", "-137.68477107274975", 
                                                "-296.07559218703756", "1092.0390648120747", "1156.2866848179935", 
                                                "-1975.0222255105061", "-2549.8125184965966", "-2608.2434152116011"
                                   ), Key = c("Actuals Sales", "Actuals Sales", "Actuals Sales", 
                                              "Actuals Sales", "Actuals Sales", "Actuals Sales", "Quarter Sales", 
                                              "Quarter Sales", "Quarter Sales", "Quarter Sales", "Quarter Sales", 
                                              "Quarter Sales")), .Names = c("Segment", "Product", "Value", 
                                                                            "Key"), row.names = c(NA, -12L), class = "data.frame")

library(tidyr)

data %>% pivot_wider(names_from = Key, values_from = Value, id_cols = c(Segment, Product))

#> # A tibble: 6 x 4
#>   Segment      Product `Actuals Sales`     `Quarter Sales`    
#>   <chr>        <chr>   <chr>               <chr>              
#> 1 non-domestic S1      517.50760307564053  -296.07559218703756
#> 2 non-domestic S2      1235.3088913918129  1092.0390648120747 
#> 3 non-domestic S3      2141.6841816176966  1156.2866848179935 
#> 4 domestic     S1      -958.38836859580044 -1975.0222255105061
#> 5 domestic     S2      -1129.5593769492507 -2549.8125184965966
#> 6 domestic     S3      -137.68477107274975 -2608.2434152116011

Тем не менее, если ваши фактические данные также содержат пробелы, вы можете использовать stringr::str_trim() перед поворотом.

data <- structure(list(Segment = c("non-domestic", "non-domestic", "non-domestic", 
                                   "domestic", "domestic", "domestic", "non-domestic ", "non-domestic ", 
                                   "non-domestic ", "domestic ", "domestic ", "domestic "), Product = c("S1", 
                                                                                                        "S2", "S3", "S1", "S2", "S3", "S1", "S2", "S3", "S1", "S2", "S3"
                                   ), Value = c("517.50760307564053", "1235.3088913918129", "2141.6841816176966", 
                                                "-958.38836859580044", "-1129.5593769492507", "-137.68477107274975", 
                                                "-296.07559218703756", "1092.0390648120747", "1156.2866848179935", 
                                                "-1975.0222255105061", "-2549.8125184965966", "-2608.2434152116011"
                                   ), Key = c("Actuals Sales", "Actuals Sales", "Actuals Sales", 
                                              "Actuals Sales", "Actuals Sales", "Actuals Sales", "Quarter Sales", 
                                              "Quarter Sales", "Quarter Sales", "Quarter Sales", "Quarter Sales", 
                                              "Quarter Sales")), .Names = c("Segment", "Product", "Value", 
                                                                            "Key"), row.names = c(NA, -12L), class = "data.frame")
library(tidyverse)
data %>% mutate(Segment = str_trim(Segment)) %>%
  pivot_wider(names_from = Key, values_from = Value, id_cols = c(Segment, Product))
#> # A tibble: 6 x 4
#>   Segment      Product `Actuals Sales`     `Quarter Sales`    
#>   <chr>        <chr>   <chr>               <chr>              
#> 1 non-domestic S1      517.50760307564053  -296.07559218703756
#> 2 non-domestic S2      1235.3088913918129  1092.0390648120747 
#> 3 non-domestic S3      2141.6841816176966  1156.2866848179935 
#> 4 domestic     S1      -958.38836859580044 -1975.0222255105061
#> 5 domestic     S2      -1129.5593769492507 -2549.8125184965966
#> 6 domestic     S3      -137.68477107274975 -2608.2434152116011

Создано 2021-06-11 пакетом REPEX (v2.0.0)

person AnilGoyal    schedule 11.06.2021
comment
Кажется, ваш dplyr навык более продвинут, чем базовый R: P - person ThomasIsCoding; 11.06.2021
comment
@ThomasIsCoding,: D, если правильно выразиться, мои базовые навыки R хуже, чем tidyverse - person AnilGoyal; 11.06.2021

удалите ненужные пробелы (trimws()) и увеличьте

library(data.table)
dcast(setDT(mydata), trimws(Segment) + Product ~ Key, value.var = "Value", fill = NA)
#         Segment Product       Actuals Sales       Quarter Sales
# 1:     domestic      S1 -958.38836859580044 -1975.0222255105061
# 2:     domestic      S2 -1129.5593769492507 -2549.8125184965966
# 3:     domestic      S3 -137.68477107274975 -2608.2434152116011
# 4: non-domestic      S1  517.50760307564053 -296.07559218703756
# 5: non-domestic      S2  1235.3088913918129  1092.0390648120747
# 6: non-domestic      S3  2141.6841816176966  1156.2866848179935
person Wimpel    schedule 11.06.2021

Базовый вариант R с использованием reshape

reshape(
  transform(data, Segment = trimws(Segment)),
  direction = "wide",
  idvar = c("Segment", "Product"),
  timevar = "Key"
)

дает

       Segment Product Value.Actuals Sales Value.Quarter Sales
1 non-domestic      S1  517.50760307564053 -296.07559218703756
2 non-domestic      S2  1235.3088913918129  1092.0390648120747
3 non-domestic      S3  2141.6841816176966  1156.2866848179935
4     domestic      S1 -958.38836859580044 -1975.0222255105061
5     domestic      S2 -1129.5593769492507 -2549.8125184965966
6     domestic      S3 -137.68477107274975 -2608.2434152116011
person ThomasIsCoding    schedule 11.06.2021

Я бы сделал это с пакетом data.table и сгенерировал 2 таблицы, а затем объединил их.

Надеюсь, этот код вам поможет.

library(data.table)

#"test" is your data frame input
test <- data.table(test)

a <- test[Key=="ActualsSales",.(Segment=Segment, Product=Product, ActualsSales=Value)]
b <- test[Key=="QuarterSales",.(Segment=Segment, Product=Product, QuarterSales=Value)]

output <- merge(a,b, by=c("Segment","Product"))
print(output)
person wiebke    schedule 11.06.2021