r-错误:"mutate()"输入"Value"出现问题.x需要数字/复杂矩阵/



当我试图将一个列命名值与1000相乘时,我遇到了一个错误。我喜欢用这种方式做这件事。

这是我的数据类型:

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

不确定为什么会出现此错误。这有点奇怪,但可能是因为我使用了变异?我尝试了总结,但没有成功。

名为value的列的数据类型为字符。尝试将其更改为数字或双精度,然后可以使用mutate或summary将其相乘。示例:

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

最新更新