我很难使用我的 R 代码。我正在尝试根据我已经拥有的数据框创建一个新的数据框,其中每个重复值单独乘以 1000 并按顺序加 1。例如,我的数据框中的值范围从 3869014 到 4524673,每个数字有多个值(最多 100 个)。例如:[3869014,3869014,3869014,3869014,3869014,3869014,3869014,3869014,3869014,3869015,3869015,3869015,3869015 What I want is: [3869014001, 3869014002, 3869014003, 3869014004, 3869014005, 3869014006, 3869014007, 3869014008, 3869015001, 3869015002, 3869015003, 3869015004, 3869016001, 3869016002, 3869016003, 3869016004, etc...]
我尝试了以下代码,但它将每个数字乘以 1000 并添加一个而不考虑重复项。它也只添加一个,而不是添加一个计数(例如:1、2、3、4 等......)。所以输出是 [3869014001, 3869014001, 3869014001, 3869014001, 等等......这不是我想要的。我对在 R 数据帧中循环有点陌生。谢谢您的帮助。
setwd("F:/TimData/SPAM/Ethiopia")
#clear all variables
rm(list=ls())
#install packages
install.packages(c("spatstat","maptools","lattice","sp","RColorBrewer","splancs","maps", "plyr"))
install.packages(c("rgdal","raster","R.utils","spsurvey", "xlsx", "rJava", "foreign"),dep=TRUE)
#load libraries
library(spatstat); library(maptools); library(lattice); library(sp);
library(RColorBrewer); library(splancs); library(maps)
library(rgdal); library(raster); library(R.utils); library(spsurvey); library(foreign);
library(rJava)
library(xlsx)
library(plyr)
#creating a custom 1km spatial grid
kmgrid = readGDAL("EthiopiaBuffer1km.tif")
#convert raster to data frame
kmgridx= as.data.frame(kmgrid, row.names=NULL, optional=FALSE, xy=FALSE, na.rm=TRUE)
#specify column containing raster values
x=kmgridx$band1
#setting counter for while statement, based on actual min/max values of raster #grid
start = 3869014
finish = 4525673
#setting loop to multiply each duplicate by 1000 and add one, doesn't work
while (start < finish) {
if (start) {
for (i in 1:length(x)) {y=(x*1000)+1}
start=start +1 }
}