使用as.numeric将char列转换为数值时出错

时间:2019-02-07 15:36:51

标签: r

我正在使用此命令读取.csv文件

data <-read.csv("906-ams2-6-194-27 PM.csv", stringsAsFactors = FALSE)

我得到以下结构

'data.frame':   2329 obs. of  11 variables:
 $ report_date     : chr  "2019-01-21" "2019-01-19" "2019-01-20" "2019-0119" ...
 $ dayofweek       : chr  "2" "7" "1" "7" ...
 $ dayname         : chr  "Monday" "Saturday" "Sunday" "Saturday" ...
 $ campaign_id     : chr  "18294432440196" "88049225507904" "122815947159809" "122815947159809" ...
 $ campaign_name   : chr  "SP | Category |Chef's Kitchen - Charcoal| Q4 18 Holiday Toy List | CPC" "SP | Auto |Shopping Cart| Q4 18 Holiday Toy List | CPC" "SP | Category |Scoop & Serve Ice Cream Counter| Q4 18 Holiday Toy List | CPC" "SP | Category |Scoop & Serve Ice Cream Counter| Q4 18 Holiday Toy List | CPC" ...
 $ demand_roi      : chr  "99.7775" "94.3251" "90.5170" "89.4055" ...
 $ demand_units    : chr  "9" "7" "12" "11" ...
 $ demand_unit_cost: chr  "0.9440920149530739" "0.5225004275450337" "0.8318443328258859" "0.6942791072683692" ...
 $ demand_max_cost : chr  "8.496828134577665" "3.657502992815236" "9.982131993910631" "7.637070179952061" ...
 $ actual_cost     : chr  "8.54" "6.06" "4.70" "4.71" ...
 $ actual_sales    : chr  "852.10" "571.61" "425.43" "421.10" ...

之后,我使用一些代码行将列转换为所需的数据类型

data$report_date<- as.Date(data$report_date , "%Y-%m-%d")
data$campaign_id <- as.numeric(data$campaign_id)
data$demand_units <- as.integer(data$demand_units)
data$demand_max_cost <- as.numeric(data$demand_max_cost)
data$actual_cost <- as.numeric(data$actual_cost)
data$actual_sales <- as.numeric(data$actual_sales)

除了一个以外,它们都可以工作

data$demand_units_cost <- as.numeric(data$demand_units_cost)

我收到以下错误

Error in `$<-.data.frame`(`*tmp*`, demand_units_cost, value = numeric(0)) : 
replacement has 0 rows, data has 2329

我当时认为数据的结构不同,但与其他列相同,带有“。”的数字。小数位

有人知道为什么它不起作用吗?可能是该列的数据中隐藏了一些空格或类似的东西吗?

dput(head(data))
structure(list(report_date = structure(c(17917, 17915, 17916, 
17915, 17915, 17916), class = "Date"), dayofweek = c("2", "7", 
"1", "7", "7", "1"), dayname = c("Monday", "Saturday", "Sunday", 
"Saturday", "Saturday", "Sunday"), campaign_id = c(18294432440196, 
88049225507904, 122815947159809, 122815947159809, 205518810606808, 
223473053729959), campaign_name = c("SP | Category |Chef's Kitchen - Charcoal| Q4 18 Holiday Toy List | CPC", 
"SP | Auto |Shopping Cart| Q4 18 Holiday Toy List | CPC", "SP | Category |Scoop & Serve Ice Cream Counter| Q4 18 Holiday Toy List | CPC", 
"SP | Category |Scoop & Serve Ice Cream Counter| Q4 18 Holiday Toy List | CPC", 
"SP | Auto |Scoop & Serve Ice Cream Counter| Q4 18 Holiday Toy List | CPC", 
"SP | Category |Star Diner Restaurant Play Set| Q4 18 Holiday Toy List | CPC"
), demand_roi = c("99.7775", "94.3251", "90.5170", "89.4055", 
"75.8974", "74.0860"), demand_units = c(9L, 7L, 12L, 11L, 14L, 
7L), demand_unit_cost = c("0.9440920149530739", "0.5225004275450337", 
"0.8318443328258859", "0.6942791072683692", "1.078626507333704", 
"0.5216382156183917"), demand_max_cost = c(8.49682813457767, 
3.65750299281524, 9.98213199391063, 7.63707017995206, 15.1007711026719, 
3.65146750932874), actual_cost = c(8.54, 6.06, 4.7, 4.71, 3.8, 
6.05), actual_sales = c(852.1, 571.61, 425.43, 421.1, 288.41, 
448.22)), .Names = c("report_date", "dayofweek", "dayname", "campaign_id", 
"campaign_name", "demand_roi", "demand_units", "demand_unit_cost", 
"demand_max_cost", "actual_cost", "actual_sales"), row.names = c(NA, 
6L), class = "data.frame")

1 个答案:

答案 0 :(得分:0)

不查看实际文件就很难诊断。尝试data$demand_units_cost <- as.numeric(as.character(data$demand_units_cost))。如果这样不起作用,请尝试将csv中的列更改为第一个,然后再次处理。