簡體   English   中英

錯誤:“mutate()”輸入“Value”有問題。 x 需要數字/復數矩陣/向量參數?

[英]Error: Problem with `mutate()` input `Value`. x requires numeric/complex matrix/vector arguments?

當我嘗試將一個名為 value 的列乘以 1000 時出現錯誤。我使用 tidyverse 執行此操作,因為我更喜歡這種方式。

這是我擁有的數據類型:

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"))

這是代碼:

numbers_multipl <- test_data %>%
  dplyr::mutate(Value = 'value' %*% 1000)

這是我得到的錯誤:

錯誤: mutate()輸入Value X要求數字/復矩陣/向量參數ℹ輸入Value"value" %*% 1000

不知道為什么我收到這個錯誤。 這有點奇怪,但可能與我使用 mutate 的事實有關? 我嘗試了摘要,但沒有奏效。

名為value的列的數據類型是字符。 嘗試將其更改為數字或雙精度值,然后您可以使用 mutate 或 summarise 將其相乘。 例子 :

numbers_multipl <- test_data %>%
  mutate(new_value= as.numeric(str_replace(value, " ", "")),
         value_times_1000 = new_value*1000
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM