Запись для цикла в r для умножения строк разных фреймов данных - PullRequest
0 голосов
/ 01 ноября 2019

Мне нужно создать цикл for для умножения значений для строк с одинаковым идентификатором в двух разных таблицах Excel (всего 154 строки).
После этого умножения полученный вектор должен использоваться длядругое умножение и деление потом. Мне нужно выполнить эти шаги 154 раза, и мне нужно сделать это с помощью цикла for.

Мне нужно рассчитать количество внесения удобрений N, P, K на один га. Для этого у меня есть два листа Excel (данные и передача). «Данные» содержат количество пакетов, внесенных для каждого удобрения N, P, K, идентификационный номер и общую площадь. «Передача» содержит вес одного пакета и общее содержание N, P, K для каждого внесенного удобрения.

#create sample data frame containig amount of bags for different fertilizer  
dataWs <- data.frame(c(1,0,3,0,0), c(2,0,3,2,0), c(2,0,4,2,0), c(4,0,1,4,0), c(2,0,2,3,0)) 
names(dataWs) <- c("urea50","urea25","diAmmPhos50","c202015_50","kali50")

# sample data frame
dataWs 
  urea50 urea25 diAmmPhos50 c202015_50 kali50
1      1      2           2          4      2
2      0      0           0          0      0
3      3      3           4          1      2
4      0      2           2          4      3
5      0      0           0          0      0


# get and rename the first row of the sample data frame
amountBag <- dataWs[1,] 

# load data frame "weightBag" for the multiplication of the weight of the   bags
weightBag <- data.frame(50,25,50,50,50)

# multiplicate dataWs and transfer and give a name for the output
amount <- (amountBag*weightBag)

#load data frame with the nitrate content for the five sample fertilizers
nitrate <- data.frame(0.46,0.463,0,0,0)

#create data frame with the total area (need for division)
area <- data.frame(2,3,3,3,1.10)

#get the total amount of nitrate for dataWs for one hectare
sumNitrate <- (amount*nitrate/area[1])

#create a for loop for row after row calculation
for (i in 1:nrow(dataWs)) {
   amountBag <- dataWs[i,] 
   amount <- (amountBag*weigthBag)
   print(amount)
 }

#need a for loop for the amount vector to be multiplicated with the vector nitrate and the result has to divided by area
# need this for loop to "loop" over every row of the data frame "amount"
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...