微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何用“,”将“价格”列整理为数字格式?例如 1,250,000 到 1250000R 编程

如何解决如何用“,”将“价格”列整理为数字格式?例如 1,250,000 到 1250000R 编程

我尝试通过以下代码删除价格列中的逗号:

property2 %>%
    select(Price) %>%
    str_remove_all(",")

但结果是返回这样的:

\" \"525000\" \"300000\" \"490000\" \"4100000\" \"750000\" \"2130000\" \"585000\" \"2480000\" \"710000\" \"565000\" \"1400000\" \"880000\" \"3500000\" \"1230000\" \"3150000\" \"499000\" \"480000\" \"475000\" \"2700000\" \"6500000\" \"5100000\" \"5000000\" \"5500000\" \"480000\" \"540000\")"
Warning message:
In stri_replace_all_regex(string,pattern,fix_replacement(replacement),:
  argument is not an atomic vector; coercing

数据信息

      Location Price Rooms add_rooms Bathrooms `Car Parks`
   <chr>    <chr> <chr> <chr>         <dbl>       <dbl>
 1 KLCC     1,25~ 2     1                 3           2
 2 damansa~ 6,80~ 6     NA                7          NA
 3 Dutamas  1,03~ 3     NA                4           2
 4 Cheras   NA    NA    NA               NA          NA
 5 Bukit J~ 900,~ 4     1                 3           2
 6 Taman T~ 5,35~ 4     2                 5           4
 7 Seputeh  NA    NA    NA               NA          NA
 8 Taman T~ 2,60~ 5     NA                4           4
 9 Taman T~ 1,95~ 4     1                 4           3
10 Sri Pet~ 385,~ 3     NA                2           1

解决方法

您可以使用 gsub 删除“,”,然后转换为数值向量。

test_df = structure(list(Price = c("1,250,000","6,800,"1,030,NA,"900,000")),row.names = c(NA,-5L),class = c("tbl_df","tbl","data.frame"))

#remove commas
test_df$Price <-gsub(",","",test_df$Price)

#Change to numeric
test_df$Price <- as.numeric(test_df$Price)

test_df$Price

或tidyverse风格

test_df = structure(list(Price = c("1,"data.frame"))

test_df <- test_df %>% 
  mutate(Price = gsub(",Price)%>%
           as.numeric)

test_df$Price
,

您可以使用 lapply 和 readr::parse_number,归功于:How to read data when some numbers contain commas as thousand separator?

#your data
test_df = structure(list(Price = c("1,"data.frame"))

test_df$Price<- lapply(test_df$Price,readr::parse_number)
test_df

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。