> new_data2 <- transform(new_data2, Percent.Non.Electric.Vehicl = (Non.Electric.Vehicle.Total/Total.Vehicles)) > ncol(new_data2) [1] 9 > new_data2 <- cbind(new_data2[1:9], Percent.Non.Electric.Vehicl = Percent.Non.Electric.Vehicl, new_data2[10:ncol(new_data2)]) Error: object 'Percent.Non.Electric.Vehicl' not found
时间: 2024-03-14 20:42:51 浏览: 23
这个错误提示是因为在 `cbind()` 函数中的 `Percent.Non.Electric.Vehicl` 变量未被定义。在 `transform()` 函数中定义的变量默认只存在于该函数中,并不会被保留下来。
你需要把 `transform()` 函数中创建的新变量与原始数据合并,然后再使用 `cbind()` 函数将它们组合起来。可以使用 `merge()` 函数或者 `dplyr` 包中的 `mutate()` 函数来实现。
示例代码:
```R
library(dplyr)
new_data2 <- merge(new_data2,
new_data2 %>%
transmute(Percent.Non.Electric.Vehicl = Non.Electric.Vehicle.Total/Total.Vehicles),
by = c("Region", "State"))
# 或者使用 mutate() 函数
new_data2 <- new_data2 %>%
mutate(Percent.Non.Electric.Vehicl = Non.Electric.Vehicle.Total/Total.Vehicles)
# 最后使用 cbind() 函数组合
new_data2 <- cbind(new_data2[, 1:9],
new_data2[, ncol(new_data2)],
new_data2[, 10:(ncol(new_data2)-1)])
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)