我有5個不同框中的單位的xyz座標的數據框,所有4x4x8都有128個可能的位置。單位都是不同的長度。所以,儘管我知道裝置的座標(3個單位,2個左邊和1個),但我不知道裝置在盒子中的確切位置(12英寸,14英尺,30英尺?)。 。 z尺寸對應於長度並且是我感興趣的尺寸。根據ID中的R來計算以前的行,for循環vs應用
我的直覺是運行一個for循環求和值,但這通常不是R中最有效的.for for循環的關鍵元素是東西沿着線:
master$unitstartpoint<-if(master$unitz)==1 0
master$unitstartpoint<-if(master$unitz)>1 master$unitstartpoint[i-1] + master$length[i-1]
即單元起始點爲0,如果它是在z維度上的第一,否則是在現有單元+現有單元的長度的開始點。這裏的數據:
# generate dataframe
master<-c(rep(1,128),rep(2,128),rep(3,128),rep(4,128),rep(5,128))
master<-as.data.frame(master)
# input basic data--what load number the unit was in, where it was located
# relative other units
master$boxNumber<-master$master
master$unitx<-rep(c(rep(1,32),rep(2,32),rep(3,32),rep(4,32)),5)
master$unity<-c(rep(1,8),rep(2,8),rep(3,8),rep(4,8))
master$unitz<-rep(1:8,80)
# create unique unit ID # based on load number and xyz coords.
transform(master,ID=paste0(boxNumber,unitx,unity,unitz))
# generate how long the unit is. this length will be used to identify unit
# location in the box
master$length<-round(rnorm(640,13,2))
我猜有一個比較容易的方法與apply
或by
要做到這一點,但我不熟悉這些功能。
其他信息:單元ID是唯一的,主數據框分別按boxNumber,unitx,unity和unitz排序。
這是我拍攝的:
length unitx unity unitz unitstartpoint
15 1 1 1 0
14 1 1 2 15
11 1 1 3 29
13 1 1 4 40
任何指導,將不勝感激。謝謝!
請添加所需結果的小例子。 –