Random Forest Presence and Absence of Data Acquisition for Help

There is already data that exists, and I don't know how to derive the data that doesn't exist, so what should I do?

Hi, welcome to the forum.

We really need to see your code , perhaps some sample data, and any error messages you are getting

See FAQ: How to do a minimal reproducible example ( reprex ) for beginners.

Rather than a reprex you also can just copy the code you are using and paste it here between

```

```

A handy way to supply data is to use the dput() function. Do dput(mydata) where "mydata" is the name of your dataset. For really large datasets probably dput(head(mydata, 100)) will do. Paste the output between
```

```

1 Like

library(dplyr)
library(sp)

确保数据框加载正确

data <- read.csv("C:/Users/zm/Desktop/RFxuexi/nine/2.csv") # 替换为实际路径
names(data) <- c("LONG", "LAT", "X2020", "aspect", "BC1", "BC10","BC11","BC12","BC13","BC14","BC15","BC16","BC17","BC18","BC19","BC2","BC3","BC4","BC5","BC6","BC7","BC8","BC9","dem","slope","srad1","srad10","srad11","srad12","srad2","srad3","srad4","srad5","srad6","srad7","srad8","srad9","TOTAL_N") # 修改为实际列名

原始数据框

data <- data.frame(id = 1:399, latitude = runif(399, 0, 90))

假设生成的新数据

new_latitude <- numeric(0) # 假设不小心生成了空向量

修复方案:提供默认值

data$latitude <- if (length(new_latitude) == 0) data$latitude else new_latitude

确保经纬度列是数值型

data$latitude <- as.numeric(data$latitude)
data$longitude <- as.numeric(data$longitude)
错误于$<-.data.frame(*tmp*, longitude, value = numeric(0)):
替换数据里有0行,但数据有399

Thank you for the code but you have a mess here. I do not see any way for the code to work.

If you can supply some sample data ant a short description of what you are doing we should be able to help.

Commented code.

Load libraries

library(dplyr)
library(sp)

Now we load and re-name the data except we do not have the .csv file with the raw data. It is on your hard drive and there is no way we can access it.

See example of using dput() function below to supply data.

data <- read.csv("C:/Users/zm/Desktop/RFxuexi/nine/2.csv")

names(data) <- c("LONG", "LAT", "X2020", "aspect", "BC1", "BC10","BC11","BC12",
                 "BC13","BC14","BC15","BC16","BC17","BC18","BC19","BC2","BC3","BC4",
                 "BC5","BC6","BC7","BC8","BC9","dem","slope","srad1","srad10","srad11",
                 "srad12","srad2","srad3","srad4","srad5","srad6","srad7","srad8","srad9",
                 "TOTAL_N")

Now we over-write the raw data file and create a uniform random sample. The original "data" data.frame no longer exists.

data <- data.frame(id = 1:399, latitude = runif(399, 0, 90))

Next we create a new one element vector with a value of zero (0) and a length of 1.

new_latitude <- numeric(0) 
length(new_latitude)

We now have a line of code that cannot work because length(new_latitude) cannot equal zero.


data1$latitude <- if (length(new_latitude) == 0) data$latitude else new_latitude

You probably do not need this but it should do no harm.

data1$latitude <- as.numeric(data$latitude)
data1$longitude <- as.numeric(data$longitude)

dput() example

dat1 <- data.frame(aa = LETTERS[1:10], bb = c(2, 5, 8, 2, 6, 9, 6, 1, 9,  3), cc = 1:10)

dput(dat1)

Output

structure(list(aa = c("A", "B", "C", "D", "E", "F", "G", "H", 
"I", "J"), bb = c(2, 5, 8, 2, 6, 9, 6, 1, 9, 3), cc = 1:10), class = "data.frame", row.names = c(NA, 
-10L))

Copy this and past into message between

```

```

Thanks for your help, the problem has been solved and I adjusted my raw data.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.