data = read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2025/2025-10-14/food_security.csv")
## Rows: 171232 Columns: 10
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (5): Area, Item, Unit, Flag, Note
## dbl (5): Year_Start, Year_End, Value, CI_Lower, CI_Upper
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
set.seed(1234)
data_small<- data %>%
select("Item", "Value","Area") %>%
sample_n(5)
data_small
## # A tibble: 5 × 3
## Item Value Area
## <chr> <dbl> <chr>
## 1 Prevalence of undernourishment (percent) (3-year average) NA Sing…
## 2 Percentage of population using at least basic sanitation services… 94 Fiji
## 3 Prevalence of undernourishment (percent) (annual value) 2.49 West…
## 4 Number of obese adults (18 years and older) (million) 1.2 Demo…
## 5 Percentage of population using at least basic drinking water serv… 99 Unit…
data_wide<- data_small %>%
pivot_wider(names_from = Area,
values_from = Value)
data_wide
## # A tibble: 5 × 6
## Item Singapore Fiji `Western Europe` Democratic Republic …¹
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 Prevalence of underno… NA NA NA NA
## 2 Percentage of populat… NA 94 NA NA
## 3 Prevalence of underno… NA NA 2.49 NA
## 4 Number of obese adult… NA NA NA 1.2
## 5 Percentage of populat… NA NA NA NA
## # ℹ abbreviated name: ¹`Democratic Republic of the Congo`
## # ℹ 1 more variable: `United States of America` <dbl>
data_long <- data_wide %>%
pivot_longer(cols = -Item, values_drop_na = TRUE) %>%
select(Item, Value = value, Area = name)
data_long
## # A tibble: 4 × 3
## Item Value Area
## <chr> <dbl> <chr>
## 1 Percentage of population using at least basic sanitation services… 94 Fiji
## 2 Prevalence of undernourishment (percent) (annual value) 2.49 West…
## 3 Number of obese adults (18 years and older) (million) 1.2 Demo…
## 4 Percentage of population using at least basic drinking water serv… 99 Unit…
data_united <- data_small %>% unite(col = "Value_Area", c(Value, Area))
data_united
## # A tibble: 5 × 2
## Item Value_Area
## <chr> <chr>
## 1 Prevalence of undernourishment (percent) (3-year average) NA_Singap…
## 2 Percentage of population using at least basic sanitation services … 94_Fiji
## 3 Prevalence of undernourishment (percent) (annual value) 2.49_West…
## 4 Number of obese adults (18 years and older) (million) 1.2_Democ…
## 5 Percentage of population using at least basic drinking water servi… 99_United…
data_longer <- data_small %>%
pivot_longer(cols =c("Area"),
names_to = ("Areas"),
values_to = "Places",
values_drop_na = TRUE)
data_longer
## # A tibble: 5 × 4
## Item Value Areas Places
## <chr> <dbl> <chr> <chr>
## 1 Prevalence of undernourishment (percent) (3-year average) NA Area Singa…
## 2 Percentage of population using at least basic sanitation s… 94 Area Fiji
## 3 Prevalence of undernourishment (percent) (annual value) 2.49 Area Weste…
## 4 Number of obese adults (18 years and older) (million) 1.2 Area Democ…
## 5 Percentage of population using at least basic drinking wat… 99 Area Unite…
```