0

I have this dataframe here of the countries in the world:

structure(list(long = c(290.100891113281, 290.104309082031, 290.057800292969, 
289.995849609375, 289.933868408203, 289.949127197266, 289.964874267578, 
290.02685546875, 290.088195800781, 290.100891113281, 74.8913116455078, 
74.8402328491211, 74.7673797607422, 74.7389602661133, 74.7266616821289, 
74.6689453125, 74.5589904785156, 74.3721694946289, 74.3761672973633, 
74.4979553222656), lat = c(12.4520015716553, 12.4229984283447, 
12.4385251998901, 12.50048828125, 12.5469722747803, 12.5970697402954, 
12.6141109466553, 12.567626953125, 12.48046875, 12.4520015716553, 
37.2316398620605, 37.2250518798828, 37.2491722106934, 37.28564453125, 
37.2907218933105, 37.2667007446289, 37.2366218566895, 37.15771484375, 
37.1373519897461, 37.0572242736816), group = c(1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2), order = c(1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 12L, 13L, 14L, 15L, 16L, 17L, 
18L, 19L, 20L, 21L), region = c("Aruba", "Aruba", "Aruba", "Aruba", 
"Aruba", "Aruba", "Aruba", "Aruba", "Aruba", "Aruba", "Afghanistan", 
"Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", 
"Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan"), 
    subregion = c(NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_), population = c(71891L, 71891L, 71891L, 71891L, 
    71891L, 71891L, 71891L, 71891L, 71891L, 71891L, 31056997L, 
    31056997L, 31056997L, 31056997L, 31056997L, 31056997L, 31056997L, 
    31056997L, 31056997L, 31056997L)), .Names = c("long", "lat", 
"group", "order", "region", "subregion", "population"), row.names = c(1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 12L, 13L, 14L, 15L, 16L, 
17L, 18L, 19L, 20L, 21L), class = "data.frame")

In the rest of the dataframe, there are some countries with NA population values. I would like a way to insert the population values for these countries with NA population values.

For example, I want to put int the population values for USA

world %>% 
  filter(region == "USA") %>% 
  mutate(population = 298444215)

This makes a separate dataframe with only the data for USA. However, I would ideally like to mutate the population values only for USA inside the entire dataframe that I dputted above!

  • 2
    Possible duplicate of [dplyr mutate/replace on a subset of rows](https://stackoverflow.com/questions/34096162/dplyr-mutate-replace-on-a-subset-of-rows) – denis Apr 30 '18 at 08:47

3 Answers3

0

Since you are using dplyr, here is an easy solution:

df <- df %>% 
      mutate(population = case_when(region == "USA" ~ 298444215,
                                TRUE ~ as.double(population)))

You can add all of the countries you need to manually assign population values to to a single call to case_when.

tifu
  • 1,352
  • 6
  • 17
0

Without using any package, we can try

## find out the index which region is USA and population is NA
index_usa <- which(world$region == "USA" & is.na(world$population))

## fill the population column according to the index
world[index_usa, ]$population <- 298444215
jacky_learns_to_code
  • 824
  • 3
  • 11
  • 29
0

Just simple indexing works for a one-liner with base R:

world[, 'population'][world[, 'region'] == 'USA'] <- 298444215

This is saying, roughly translated in to English;

  1. world[, 'population'] for the population column
  2. [world[, 'region'] == 'USA'] at the rows where the region column is USA
  3. <- 298444215 insert this value
rg255
  • 4,119
  • 3
  • 22
  • 40