Reputation: 1132
I get an error when I am trying to multiply one column named value with 1000. I do this with tidyverse as I prefer it this way.
Here is my type of data I have:
test_data <- structure(list(Index = c("71", "71", "71", "71", "71", "71",
"71", "71", "71", "71", "71", "71", "71", "71", "71", "71", "71",
"71", "71", "71", "71"), Variant = c("Estimates", "Estimates",
"Estimates", "Estimates", "Estimates", "Estimates", "Estimates",
"Estimates", "Estimates", "Estimates", "Estimates", "Estimates",
"Estimates", "Estimates", "Estimates", "Estimates", "Estimates",
"Estimates", "Estimates", "Estimates", "Estimates"), `Region, subregion, country or area *` = c("WORLD",
"WORLD", "WORLD", "WORLD", "WORLD", "WORLD", "WORLD", "WORLD",
"WORLD", "WORLD", "WORLD", "WORLD", "WORLD", "WORLD", "WORLD",
"WORLD", "WORLD", "WORLD", "WORLD", "WORLD", "WORLD"), Notes = 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_, NA_character_
), `Country code` = c("900", "900", "900", "900", "900", "900",
"900", "900", "900", "900", "900", "900", "900", "900", "900",
"900", "900", "900", "900", "900", "900"), Type = c("World",
"World", "World", "World", "World", "World", "World", "World",
"World", "World", "World", "World", "World", "World", "World",
"World", "World", "World", "World", "World", "World"), `Parent code` = c("0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0"), year = c("2020", "2020",
"2020", "2020", "2020", "2020", "2020", "2020", "2020", "2020",
"2020", "2020", "2020", "2020", "2020", "2020", "2020", "2020",
"2020", "2020", "2020"), age_band = c("0-4", "5-9", "10-14",
"15-19", "20-24", "25-29", "30-34", "35-39", "40-44", "45-49",
"50-54", "55-59", "60-64", "65-69", "70-74", "75-79", "80-84",
"85-89", "90-94", "95-99", "100_plus"), value = c("677 942",
"664 439", "641 267", "612 196", "597 388", "594 692", "605 531",
"544 819", "493 789", "479 366", "445 773", "387 849", "322 142",
"269 644", "188 677", "123 782", "81 930", "42 186", "16 680",
"4 134", "573")), row.names = c(NA, -21L), class = c("tbl_df",
"tbl", "data.frame"))
here is the code:
numbers_multipl <- test_data %>%
dplyr::mutate(Value = 'value' %*% 1000)
And here is the error I get:
Error: Problem with
mutate()
inputValue
. x requires numeric/complex matrix/vector arguments ℹ InputValue
is"value" %*% 1000
.
Not sure why I get this error. It is a bit odd but probably it lies with the fact I am using mutate? I tried summaries and it did not work.
Upvotes: 0
Views: 382
Reputation: 97
the datatypes of column named value
is character. try to change it into numeric or double then you can multiply it using mutate or summarise.
example :
numbers_multipl <- test_data %>%
mutate(new_value= as.numeric(str_replace(value, " ", "")),
value_times_1000 = new_value*1000
)
Upvotes: 1