Loop to generate new columns and rows in a data frame

#- My formula generate only new column w1 .I want to generate 10 column w1 to w10 based on column v1 to v10(sto.pdf is sto.csv converted to pdf)
library(dplyr)
library(formattable)
library(reshape2)
library(data.table)

mac <- read.csv("D:/TEMP/sto.csv", dec=",")
mac1<- mac %>% mutate(Lung=sample(x = c(216,213,184),
size = 129, replace = TRUE))
#mac1
mac1 <- mac1 %>%
mutate(Buc = as.numeric(Buc),MT=as.numeric(MT))
mac1<-mac1%>%mutate(medie=round(MT/Buc,2))
mac1<-mac1%>%mutate(Pcs=floor((medie1000-200)/Lung))
#str(mac)
mac1<-mac1%>%mutate(CTT=MT/(Buc
Lung*Pcs/1000))

mac1<-mac1%>%mutate(util=(BucLungPcs/1000))
mac1
head(mac1)

#################

#########################
y=rep(sample(c(216,213,184),
size = 129,
replace=TRUE))
y

z= matrix(ncol = 10,nrow = 129)
for(year in 1:10) {

for each year sample the return 10000 times

for(i in 1:129){
z[i,year] = sample(y,1)
}
}
colnames(z) <- paste0("V", 1:ncol(z))
z
data_new2 <- cbind(mac1, z)
data_new2

library(tidyverse)
for(i in 1:nrow(data_new2)) {
data_new2$w1[i] <- data_new2$V1[i]/data_new2$medie

for(j in 1:ncol(data_new2)) {
data_new2$w1[j] <- data_new2$V1[j]/data_new2$medie
}
}
sto.pdf (231.9 KB)

Please format your code

ibrary(dplyr)
library(formattable)
library(reshape2)
library(data.table)

mac <- read.csv("D:/TEMP/sto1.csv", dec=",")
mac1<- mac %>% mutate(Lung=sample(x = c(216,213,184),
                                  size = 129, replace = TRUE))
mac1
mac1 <- mac1 %>% 
  mutate(Buc = as.numeric(Buc),MT=as.numeric(MT))
mac1<-mac1%>%mutate(medie=round(MT/Buc,2))
mac1<-mac1%>%mutate(Pcs=floor((medie*1000-200)/Lung))
#str(mac)
mac1<-mac1%>%mutate(CTT=MT/(Buc*Lung*Pcs/1000))

mac1<-mac1%>%mutate(util=(Buc*Lung*Pcs/1000))
mac1
head(mac1)



#################


#########################
y=rep(sample(c(216,213,184),
             size = 129,
             replace=TRUE))
y

z= matrix(ncol = 10,nrow = 129)
for(year in 1:10) {
  # for each year sample the return 10000 times
  for(i in 1:129){
    z[i,year] = sample(y,1)
  }
}
colnames(z) <- paste0("V", 1:ncol(z))
z
data_new2 <- cbind(mac1, z)
data_new2 


library(dplyr)
names(data_new2)
data_new2 %>%
  mutate(
    V1  = medie*1000/V1 ,
    V2  = medie*1000/V2)
data_new2
library(tidyverse)
for(i in 1:nrow(data_new2)) {
  data_new2$w1[i] <- data_new2$V1[i]/100

for(j in 1:ncol(data_new2)) {
  data_new2$w1[j] <- data_new2$V1[j]/10
}
}
################
library(tidyverse)


for(j in 1:ncol(data_new2)) {
  data_new2$w1[j] <- data_new2$V1[j]/100
}

Better to share the data directly with dput(mac) than post the pdf.

structure(list(Buc = c(302L, 296L, 304L, 240L, 385L, 302L, 322L, 
306L, 357L, 295L, 320L, 315L, 218L, 270L, 280L, 360L, 288L, 294L, 
308L, 283L, 302L, 209L, 312L, 210L, 314L, 305L, 195L, 279L, 229L, 
294L, 310L, 173L, 306L, 308L, 238L, 268L, 227L, 200L, 339L, 198L, 
94L, 309L, 198L, 308L, 294L, 192L, 294L, 318L, 268L, 268L, 208L, 
240L, 313L, 316L, 318L, 314L, 297L, 340L, 340L, 340L, 340L, 326L, 
295L, 320L, 340L, 340L, 67L, 99L, 135L, 64L, 126L, 318L, 316L, 
315L, 314L, 316L, 316L, 300L, 308L, 316L, 313L, 306L, 191L, 190L, 
195L, 318L, 199L, 316L, 316L, 312L, 314L, 306L, 316L, 316L, 310L, 
315L, 316L, 288L, 314L, 316L, 314L, 269L, 315L, 314L, 302L, 307L, 
316L, 313L, 288L, 339L, 354L, 70L, 280L, 316L, 304L, 303L, 299L, 
247L, 305L, 315L, 322L, 114L, 308L, 303L, 309L, 294L, 325L, 200L, 
198L), MT = c("1884.48", "1847.4", "1869.96", "1497.6", "2194.5", 
"1887.5", "2019", "1915", "2177", "1874", "1952", "1978", "1382", 
"1385", "1747", "2208", "1805", "1843.38", "1931.16", "1768", 
"1893.5", "1308.3", "1953", "1331.4", "1978.2", "1930.65", "1234.35", 
"1766.07", "1454.15", "1902.18", "1984", "1098.55", "1943.1", 
"1955.8", "1511.3", "1688.4", "1430", "1276", "2148", "1251", 
"592", "1945", "1260", "1931.16", "1878.66", "1194.24", "1878.66", 
"2032.02", "1666", "1666", "1144", "1497.6", "1972", "1987", 
"1996", "1984.5", "1856.25", "2176", "2176", "2176", "2125", 
"2038", "1843.5", "2000", "2125", "2125", "522", "504", "857", 
"400", "787.5", "2054.28", "2028.72", "2025.45", "2019.02", "2038.2", 
"2044.52", "1935", "1989", "2039", "2019", "1964.52", "1230.04", 
"1229.3", "1251.9", "2060.64", "1287", "2022.4", "2022.4", "1996.8", 
"2009.6", "1958.4", "2022.4", "2022.4", "1984", "2031", "2038.2", 
"1857.6", "2025.3", "2047", "2009.6", "1748.5", "2047.5", "2041", 
"1917.7", "1943.31", "2006.6", "1987.55", "1854.7", "2125.53", 
"2194.8", "525", "1773.8", "2005.6", "1930.4", "1920", "1894.7", 
"1568.45", "1933.7", "2000.25", "2044.7", "570", "1925", "1900", 
"1935", "1885.06", "2040.5", "1274", "1266.6"), Kg = c(2619L, 
2568L, 2599L, 2082L, 3050L, 2624L, 2806L, 2662L, 3026L, 2605L, 
2713L, 2749L, 1921L, 1925L, 2428L, 3069L, 2509L, 2562L, 2684L, 
2458L, 2632L, 1819L, 2715L, 1851L, 2750L, 2684L, 1716L, 2455L, 
2021L, 2644L, 2758L, 1527L, 2701L, 2719L, 2101L, 2347L, 1988L, 
1774L, 2986L, 1739L, 823L, 2704L, 1751L, 2684L, 2611L, 1660L, 
2611L, 2825L, 2316L, 2316L, 1590L, 2082L, 2741L, 2762L, 2774L, 
2758L, 2580L, 3025L, 3025L, 3025L, 2954L, 2833L, 2562L, 2780L, 
2954L, 2954L, 726L, 701L, 1191L, 556L, 1095L, 2855L, 2820L, 2815L, 
2806L, 2833L, 2842L, 2690L, 2765L, 2834L, 2806L, 2731L, 1710L, 
1709L, 1740L, 2864L, 1789L, 2811L, 2811L, 2776L, 2793L, 2722L, 
2811L, 2811L, 2758L, 2823L, 2833L, 2582L, 2815L, 2845L, 2793L, 
2430L, 2846L, 2837L, 2666L, 2701L, 2789L, 2763L, 2578L, 2954L, 
3051L, 730L, 2466L, 2788L, 2683L, 2669L, 2634L, 2180L, 2688L, 
2780L, 2842L, 792L, 2676L, 2641L, 2690L, 2620L, 2836L, 1771L, 
1761L)), class = "data.frame", row.names = c(NA, -129L))

Thanks, Danut — could you delete the previous post? The one where you shared the data but didn't put it in a code block?

Sorry I still don't fully understand what you want, let me try to reformulate.

You have a data.frame data_new2, which contains some columns Buc, MT, kg, ... and then some columns V1, V2, V3, ...

You want to add new columns W1, W2, ... such that Wn = Vn/100 (where n is 1:10).

Is that it?

In that case, there are a few solutions I can think of: with a for loop, with pivot_longer(), or with across().

Pure base R

Use a for loop on the columns. The difficulty is that you have to select the columns to transform and prepare column names before the loop itself:

data_new2 <- dget("data_new2.dput")

head(data_new2)
#>   Buc      MT   Kg Lung medie Pcs      CTT     util  V1  V2  V3  V4  V5  V6  V7
#> 1 302 1884.48 2619  184  6.24  32 1.059783 1778.176 184 213 184 213 213 213 184
#> 2 296 1847.40 2568  216  6.24  27 1.070167 1726.272 213 216 213 216 184 216 213
#> 3 304 1869.96 2599  216  6.15  27 1.054730 1772.928 216 213 216 184 216 184 216
#> 4 240 1497.60 2082  184  6.24  32 1.059783 1413.120 213 213 216 216 184 216 213
#> 5 385 2194.50 3050  216  5.70  25 1.055556 2079.000 216 213 216 213 216 216 213
#> 6 302 1887.50 2624  216  6.25  28 1.033399 1826.496 213 216 184 213 184 213 216
#>    V8  V9 V10
#> 1 216 213 213
#> 2 184 216 213
#> 3 216 184 213
#> 4 213 216 213
#> 5 213 216 184
#> 6 216 216 184

(colnames <- colnames(data_new2))
#>  [1] "Buc"   "MT"    "Kg"    "Lung"  "medie" "Pcs"   "CTT"   "util"  "V1"   
#> [10] "V2"    "V3"    "V4"    "V5"    "V6"    "V7"    "V8"    "V9"    "V10"

(cols_to_process <- colnames[startsWith(colnames, "V")])
#>  [1] "V1"  "V2"  "V3"  "V4"  "V5"  "V6"  "V7"  "V8"  "V9"  "V10"
(new_names <- sub("^V", "W", cols_to_process))
#>  [1] "W1"  "W2"  "W3"  "W4"  "W5"  "W6"  "W7"  "W8"  "W9"  "W10"

for(i in seq_along(cols_to_process)){
  data_new2[[ new_names[[ i ]] ]] <- data_new2[[ cols_to_process[[ i ]] ]]/100
}

head(data_new2)
#>   Buc      MT   Kg Lung medie Pcs      CTT     util  V1  V2  V3  V4  V5  V6  V7
#> 1 302 1884.48 2619  184  6.24  32 1.059783 1778.176 184 213 184 213 213 213 184
#> 2 296 1847.40 2568  216  6.24  27 1.070167 1726.272 213 216 213 216 184 216 213
#> 3 304 1869.96 2599  216  6.15  27 1.054730 1772.928 216 213 216 184 216 184 216
#> 4 240 1497.60 2082  184  6.24  32 1.059783 1413.120 213 213 216 216 184 216 213
#> 5 385 2194.50 3050  216  5.70  25 1.055556 2079.000 216 213 216 213 216 216 213
#> 6 302 1887.50 2624  216  6.25  28 1.033399 1826.496 213 216 184 213 184 213 216
#>    V8  V9 V10   W1   W2   W3   W4   W5   W6   W7   W8   W9  W10
#> 1 216 213 213 1.84 2.13 1.84 2.13 2.13 2.13 1.84 2.16 2.13 2.13
#> 2 184 216 213 2.13 2.16 2.13 2.16 1.84 2.16 2.13 1.84 2.16 2.13
#> 3 216 184 213 2.16 2.13 2.16 1.84 2.16 1.84 2.16 2.16 1.84 2.13
#> 4 213 216 213 2.13 2.13 2.16 2.16 1.84 2.16 2.13 2.13 2.16 2.13
#> 5 213 216 184 2.16 2.13 2.16 2.13 2.16 2.16 2.13 2.13 2.16 1.84
#> 6 216 216 184 2.13 2.16 1.84 2.13 1.84 2.13 2.16 2.16 2.16 1.84

Created on 2024-04-04 with reprex v2.0.2

With across()

A tidyverse approach, the idea is to use across(.cols, .fns, .names), where .cols are the Vn columns, .fns the function you want to apply (here, dividing by 100), and .names some way to name the result.

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
data_new2 <- dget("data_new2.dput")

head(data_new2)
#>   Buc      MT   Kg Lung medie Pcs      CTT     util  V1  V2  V3  V4  V5  V6  V7
#> 1 302 1884.48 2619  184  6.24  32 1.059783 1778.176 184 213 184 213 213 213 184
#> 2 296 1847.40 2568  216  6.24  27 1.070167 1726.272 213 216 213 216 184 216 213
#> 3 304 1869.96 2599  216  6.15  27 1.054730 1772.928 216 213 216 184 216 184 216
#> 4 240 1497.60 2082  184  6.24  32 1.059783 1413.120 213 213 216 216 184 216 213
#> 5 385 2194.50 3050  216  5.70  25 1.055556 2079.000 216 213 216 213 216 216 213
#> 6 302 1887.50 2624  216  6.25  28 1.033399 1826.496 213 216 184 213 184 213 216
#>    V8  V9 V10
#> 1 216 213 213
#> 2 184 216 213
#> 3 216 184 213
#> 4 213 216 213
#> 5 213 216 184
#> 6 216 216 184


mutate(data_new2,
       across(starts_with("V"),
              .fns = \(.x) .x/100,
              .names = "W{.col}")) %>%
  head()
#>   Buc      MT   Kg Lung medie Pcs      CTT     util  V1  V2  V3  V4  V5  V6  V7
#> 1 302 1884.48 2619  184  6.24  32 1.059783 1778.176 184 213 184 213 213 213 184
#> 2 296 1847.40 2568  216  6.24  27 1.070167 1726.272 213 216 213 216 184 216 213
#> 3 304 1869.96 2599  216  6.15  27 1.054730 1772.928 216 213 216 184 216 184 216
#> 4 240 1497.60 2082  184  6.24  32 1.059783 1413.120 213 213 216 216 184 216 213
#> 5 385 2194.50 3050  216  5.70  25 1.055556 2079.000 216 213 216 213 216 216 213
#> 6 302 1887.50 2624  216  6.25  28 1.033399 1826.496 213 216 184 213 184 213 216
#>    V8  V9 V10  WV1  WV2  WV3  WV4  WV5  WV6  WV7  WV8  WV9 WV10
#> 1 216 213 213 1.84 2.13 1.84 2.13 2.13 2.13 1.84 2.16 2.13 2.13
#> 2 184 216 213 2.13 2.16 2.13 2.16 1.84 2.16 2.13 1.84 2.16 2.13
#> 3 216 184 213 2.16 2.13 2.16 1.84 2.16 1.84 2.16 2.16 1.84 2.13
#> 4 213 216 213 2.13 2.13 2.16 2.16 1.84 2.16 2.13 2.13 2.16 2.13
#> 5 213 216 184 2.16 2.13 2.16 2.13 2.16 2.16 2.13 2.13 2.16 1.84
#> 6 216 216 184 2.13 2.16 1.84 2.13 1.84 2.13 2.16 2.16 2.16 1.84

Created on 2024-04-04 with reprex v2.0.2

Only problem is in the column naming, I can easily add a W, but you'd want to remove the V; you can do it after the fact with colnames().

With pivot_longer()

What's inconvenient is that you need to loop on columns. In the tidyverse logic, it's easier to loop on rows. Easy enough: just put your columns into rows.

Here however it won't work because your rows are redundant:

data_new2 <- dget("data_new2.dput")

data_new2[,1:8] |> nrow()
#> [1] 129

data_new2[,1:8] |> unique() |> nrow()
#> [1] 125

Created on 2024-04-04 with reprex v2.0.2

So there are 4 rows that are duplicates in the first 8 columns, if you pivot_longer, it becomes impossible to pivot back to wide format (we don't know which V1 belongs to which Buc, MT, ...)

The idea would be:

data_new2 %>%
  pivot_longer(cols = starts_with("V"),
               names_to = "n",
               values_to = "V") %>%
  mutate(n = str_remove(n, "V")) %>%
  mutate(W = V / 100) %>%
  pivot_wider(names_from = n,
              names_sep = "",
              values_from = c(V,W))

So, you pivot longer, you can extract the n from Vn, apply any transformation to the V column (if necessary using group_by() to transform by n), then expand back to a wide data.frame creating the columns Vn and Wn.

This should work if you don't have duplicates in the first 8 columns.

"#Instead of divide by 100 I want to divide with column" Buc " and to obtain a new adisional series of columns (Z1 to Z10 ) by multiply columns start with "W" with column "MT"

This can be done by piping the result to rename_with():

mutate(data_new2,
       across(starts_with("V"),
              .fns = \(.x) .x/100,
              .names = "W{.col}")) |>
  rename_with(
    \(name) name |> str_remove('V'),
    contains('WV')
  )
1 Like

So shouldn't be too hard to change that code, maybe something like

for(i in seq_along(cols_to_process)){
  Buc_i <- data_new2$Buc[[i]]
  MT_i <- data_new2$MT[[i]]
  data_new2[[ new_names[[ i ]] ]] <- data_new2[[ cols_to_process[[ i ]] ]]*MT_i/Buc_i
}
#Loop over columns and rows in a data frame
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(formattable)
library(reshape2)
library(data.table)
#> 
#> Attaching package: 'data.table'
#> The following objects are masked from 'package:reshape2':
#> 
#>     dcast, melt
#> The following objects are masked from 'package:dplyr':
#> 
#>     between, first, last

mac <- read.csv("D:/TEMP/sto1.csv", dec=",")
mac1<- mac %>% mutate(Lung=sample(x = c(216,213,184),
                                  size = 129, replace = TRUE))
head(mac1)
#>   Buc      MT   Kg Lung
#> 1 302 1884.48 2619  184
#> 2 296  1847.4 2568  213
#> 3 304 1869.96 2599  184
#> 4 240  1497.6 2082  184
#> 5 385  2194.5 3050  216
#> 6 302  1887.5 2624  216
mac1 <- mac1 %>% 
  mutate(Buc = as.numeric(Buc),MT=as.numeric(MT))
mac1<-mac1%>%mutate(medie=round(MT/Buc,2))
mac1<-mac1%>%mutate(Pcs=floor((medie*1000-200)/Lung))
#str(mac)
mac1<-mac1%>%mutate(CTT=MT/(Buc*Lung*Pcs/1000))

mac1<-mac1%>%mutate(util=(Buc*Lung*Pcs/1000))

head(mac1)
#>   Buc      MT   Kg Lung medie Pcs      CTT     util
#> 1 302 1884.48 2619  184  6.24  32 1.059783 1778.176
#> 2 296 1847.40 2568  213  6.24  28 1.046482 1765.344
#> 3 304 1869.96 2599  184  6.15  32 1.044698 1789.952
#> 4 240 1497.60 2082  184  6.24  32 1.059783 1413.120
#> 5 385 2194.50 3050  216  5.70  25 1.055556 2079.000
#> 6 302 1887.50 2624  216  6.25  28 1.033399 1826.496



#################


#########################
y=rep(sample(c(216,213,184),
             size = 129,
             replace=TRUE))
y
#>   [1] 184 184 213 213 216 216 184 184 213 213 184 184 184 213 216 213 184 216
#>  [19] 184 216 184 216 184 184 216 213 216 216 184 216 213 213 213 213 184 184
#>  [37] 213 184 216 213 184 184 213 216 216 216 216 184 184 216 216 184 213 184
#>  [55] 216 216 216 184 213 184 216 184 184 213 184 213 184 216 213 216 213 216
#>  [73] 184 216 213 184 216 184 213 216 213 213 216 213 216 213 216 184 213 216
#>  [91] 216 216 184 216 184 184 184 213 184 216 213 213 184 184 216 216 216 213
#> [109] 213 213 184 213 213 213 184 216 213 213 216 216 216 213 184 184 216 213
#> [127] 184 213 213

z= matrix(ncol = 10,nrow = 129)
for(year in 1:10) {
  # for each year sample the return 10000 times
  for(i in 1:129){
    z[i,year] = sample(y,1)
  }
}
colnames(z) <- paste0("V", 1:ncol(z))
head(z)
#>       V1  V2  V3  V4  V5  V6  V7  V8  V9 V10
#> [1,] 213 184 216 213 216 216 216 184 213 216
#> [2,] 184 216 216 216 213 213 216 213 184 216
#> [3,] 213 213 216 213 184 213 216 184 184 184
#> [4,] 213 213 213 184 213 216 213 184 184 216
#> [5,] 213 213 184 184 184 213 213 184 216 213
#> [6,] 213 184 216 216 216 216 216 216 213 213
data_new2 <- cbind(mac1, z)
head(data_new2)
#>   Buc      MT   Kg Lung medie Pcs      CTT     util  V1  V2  V3  V4  V5  V6  V7
#> 1 302 1884.48 2619  184  6.24  32 1.059783 1778.176 213 184 216 213 216 216 216
#> 2 296 1847.40 2568  213  6.24  28 1.046482 1765.344 184 216 216 216 213 213 216
#> 3 304 1869.96 2599  184  6.15  32 1.044698 1789.952 213 213 216 213 184 213 216
#> 4 240 1497.60 2082  184  6.24  32 1.059783 1413.120 213 213 213 184 213 216 213
#> 5 385 2194.50 3050  216  5.70  25 1.055556 2079.000 213 213 184 184 184 213 213
#> 6 302 1887.50 2624  216  6.25  28 1.033399 1826.496 213 184 216 216 216 216 216
#>    V8  V9 V10
#> 1 184 213 216
#> 2 213 184 216
#> 3 184 184 184
#> 4 184 184 216
#> 5 184 216 213
#> 6 216 213 213

(colnames <- colnames(data_new2))
#>  [1] "Buc"   "MT"    "Kg"    "Lung"  "medie" "Pcs"   "CTT"   "util"  "V1"   
#> [10] "V2"    "V3"    "V4"    "V5"    "V6"    "V7"    "V8"    "V9"    "V10"

(cols_to_process <- colnames[startsWith(colnames, "V")])
#>  [1] "V1"  "V2"  "V3"  "V4"  "V5"  "V6"  "V7"  "V8"  "V9"  "V10"

for(i in seq_along(cols_to_process)){
  Buc_i <- data_new2$Buc[[i]]
  MT_i <- data_new2$MT[[i]]
  
  data_new2[[ new_names[[ i ]] ]] <- data_new2[[ cols_to_process[[ i ]] ]]*MT_i/Buc_i
}
#> Error in eval(expr, envir, enclos): object 'new_names' not found

head(data_new2)
#>   Buc      MT   Kg Lung medie Pcs      CTT     util  V1  V2  V3  V4  V5  V6  V7
#> 1 302 1884.48 2619  184  6.24  32 1.059783 1778.176 213 184 216 213 216 216 216
#> 2 296 1847.40 2568  213  6.24  28 1.046482 1765.344 184 216 216 216 213 213 216
#> 3 304 1869.96 2599  184  6.15  32 1.044698 1789.952 213 213 216 213 184 213 216
#> 4 240 1497.60 2082  184  6.24  32 1.059783 1413.120 213 213 213 184 213 216 213
#> 5 385 2194.50 3050  216  5.70  25 1.055556 2079.000 213 213 184 184 184 213 213
#> 6 302 1887.50 2624  216  6.25  28 1.033399 1826.496 213 184 216 216 216 216 216
#>    V8  V9 V10
#> 1 184 213 216
#> 2 213 184 216
#> 3 184 184 184
#> 4 184 184 216
#> 5 184 216 213
#> 6 216 213 213

Created on 2024-04-05 with reprex v2.0.2

Yep, you didn't create it. See my previous message.

Rigth

One question :

I want to generate new columns X1 to X10  as results of  Buc*V1 to V10*W1 to W10

(colnames <- colnames(data_new2))


(cols_to_process <-colnames( data_new2 %>% select(V1:W10)))
(new_names <- sub("^W", "x", cols_to_process))
for(i in seq_along(cols_to_process)){
  V_i<-data_new2$V[[i]]
    W_i<-data_new2$W[[i]]
  Buc_i<-data_new2$Buc[[i]]
    data_new2[[ new_names[[ i ]] ]] <-Buc_i *data_new2[[ cols_to_process[[ i ]] ]]

HI Danut,

I'm curious — would you mind running sessionInfo() and post the output? That would help identify which solutions are better suited to your setup.

You can adapt new_names <- sub("^W", "x", cols_to_process) to create new column names X1, X2, ... and code it explicitly. Is it clear to you what the for loop is doing?

Clear for what loop are doing .How to do?

I have a suggestion: It looks like you are trying to do something that is simple to describe through an example, but may be complicated to achieve in practice, so I would recommend you:

  1. Build a very simple "before" table that is enough to illustrate what you are trying to do. For example, my sense is that starting with a table like this is probably enough:
library(tibble)
tibble(
  a = 2,
  V1 = 1:3,
  V2 = 3:1
) 
#> # A tibble: 3 × 3
#>       a    V1    V2
#>   <dbl> <int> <int>
#> 1     2     1     3
#> 2     2     2     2
#> 3     2     3     1

Created on 2024-04-06 with reprex v2.0.2

  1. Then — by hand, when necesarry — build and "after" table that illustrates the end result you would like to achieve:
tibble(
  a = 2,
  V1 = 1:3,
  V2 = 3:1,
  W1 = c(1/2, 2/2, 3/2), #<-- V1 / a
  W2 = c(3/2, 2/2, 1/2)  #<-- V2 / a
)
#> # A tibble: 3 × 5
#>       a    V1    V2    W1    W2
#>   <dbl> <int> <int> <dbl> <dbl>
#> 1     2     1     3   0.5   1.5
#> 2     2     2     2   1     1  
#> 3     2     3     1   1.5   0.5

This before-and-after approach to asking questions makes it much easier for folks to understand what a question-poster is trying to achieve, and is also an important step for the question-poster in clarifying their issue or question for themselves — the more details, the harder it is to describe and understand the question.

``` r
#Loop over columns and rows in a data frame
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(formattable)
library(reshape2)
library(data.table)
#> 
#> Attaching package: 'data.table'
#> The following objects are masked from 'package:reshape2':
#> 
#>     dcast, melt
#> The following objects are masked from 'package:dplyr':
#> 
#>     between, first, last

mac <- read.csv("D:/TEMP/sto1.csv", dec=",")
mac1<- mac %>% mutate(Lung=sample(x = c(216,213,184),
                                  size = 129, replace = TRUE))
head(mac1)
#>   Buc      MT   Kg Lung
#> 1 302 1884.48 2619  184
#> 2 296  1847.4 2568  216
#> 3 304 1869.96 2599  216
#> 4 240  1497.6 2082  184
#> 5 385  2194.5 3050  216
#> 6 302  1887.5 2624  184
mac1 <- mac1 %>% 
  mutate(Buc = as.numeric(Buc),MT=as.numeric(MT))
mac1<-mac1%>%mutate(medie=round(MT/Buc,2))
mac1<-mac1%>%mutate(Pcs=floor((medie*1000-200)/Lung))
#str(mac)
mac1<-mac1%>%mutate(CTT=MT/(Buc*Lung*Pcs/1000))

mac1<-mac1%>%mutate(util=(Buc*Lung*Pcs/1000))

head(mac1)
#>   Buc      MT   Kg Lung medie Pcs      CTT     util
#> 1 302 1884.48 2619  184  6.24  32 1.059783 1778.176
#> 2 296 1847.40 2568  216  6.24  27 1.070167 1726.272
#> 3 304 1869.96 2599  216  6.15  27 1.054730 1772.928
#> 4 240 1497.60 2082  184  6.24  32 1.059783 1413.120
#> 5 385 2194.50 3050  216  5.70  25 1.055556 2079.000
#> 6 302 1887.50 2624  184  6.25  32 1.061481 1778.176
#################


#########################
y=rep(sample(c(216,213,184),
             size = 129,
             replace=TRUE))
y
#>   [1] 184 213 213 184 184 184 213 213 213 216 213 213 213 184 184 213 216 213
#>  [19] 213 184 184 184 184 184 213 213 213 213 216 184 184 216 216 216 184 213
#>  [37] 216 216 213 216 213 184 213 184 216 216 216 216 184 216 184 213 184 184
#>  [55] 184 184 213 213 213 216 184 216 213 216 184 184 213 216 213 216 216 184
#>  [73] 184 184 213 213 216 213 216 216 213 213 213 213 216 184 213 216 216 213
#>  [91] 213 216 184 213 213 213 213 216 184 216 216 213 213 184 216 216 184 216
#> [109] 216 216 213 213 213 184 213 216 216 184 216 184 216 213 184 213 216 216
#> [127] 213 184 216

z= matrix(ncol = 10,nrow = 129)
for(year in 1:10) {
  # for each year sample the return 10000 times
  for(i in 1:129){
    z[i,year] = sample(y,1)
  }
}
colnames(z) <- paste0("V", 1:ncol(z))
head(z)
#>       V1  V2  V3  V4  V5  V6  V7  V8  V9 V10
#> [1,] 216 213 216 213 216 216 184 184 184 216
#> [2,] 216 184 213 184 184 184 216 213 216 213
#> [3,] 216 184 213 184 216 184 213 213 184 184
#> [4,] 216 213 184 216 216 184 216 216 213 216
#> [5,] 213 213 216 216 213 216 213 216 216 216
#> [6,] 213 213 213 184 213 216 213 213 184 213
data_new2 <- cbind(mac1, z)
head(data_new2)
#>   Buc      MT   Kg Lung medie Pcs      CTT     util  V1  V2  V3  V4  V5  V6  V7
#> 1 302 1884.48 2619  184  6.24  32 1.059783 1778.176 216 213 216 213 216 216 184
#> 2 296 1847.40 2568  216  6.24  27 1.070167 1726.272 216 184 213 184 184 184 216
#> 3 304 1869.96 2599  216  6.15  27 1.054730 1772.928 216 184 213 184 216 184 213
#> 4 240 1497.60 2082  184  6.24  32 1.059783 1413.120 216 213 184 216 216 184 216
#> 5 385 2194.50 3050  216  5.70  25 1.055556 2079.000 213 213 216 216 213 216 213
#> 6 302 1887.50 2624  184  6.25  32 1.061481 1778.176 213 213 213 184 213 216 213
#>    V8  V9 V10
#> 1 184 184 216
#> 2 213 216 213
#> 3 213 184 184
#> 4 216 213 216
#> 5 216 216 216
#> 6 213 184 213
#ADAUGAT
head(data_new2)
#>   Buc      MT   Kg Lung medie Pcs      CTT     util  V1  V2  V3  V4  V5  V6  V7
#> 1 302 1884.48 2619  184  6.24  32 1.059783 1778.176 216 213 216 213 216 216 184
#> 2 296 1847.40 2568  216  6.24  27 1.070167 1726.272 216 184 213 184 184 184 216
#> 3 304 1869.96 2599  216  6.15  27 1.054730 1772.928 216 184 213 184 216 184 213
#> 4 240 1497.60 2082  184  6.24  32 1.059783 1413.120 216 213 184 216 216 184 216
#> 5 385 2194.50 3050  216  5.70  25 1.055556 2079.000 213 213 216 216 213 216 213
#> 6 302 1887.50 2624  184  6.25  32 1.061481 1778.176 213 213 213 184 213 216 213
#>    V8  V9 V10
#> 1 184 184 216
#> 2 213 216 213
#> 3 213 184 184
#> 4 216 213 216
#> 5 216 216 216
#> 6 213 184 213
library(tidyverse)
#> Warning: package 'readr' was built under R version 4.3.2
library(conflicted)
library(dplyr)
(colnames <- colnames(data_new2))
#>  [1] "Buc"   "MT"    "Kg"    "Lung"  "medie" "Pcs"   "CTT"   "util"  "V1"   
#> [10] "V2"    "V3"    "V4"    "V5"    "V6"    "V7"    "V8"    "V9"    "V10"
(cols_to_process <- colnames[startsWith(colnames, "V")])
#>  [1] "V1"  "V2"  "V3"  "V4"  "V5"  "V6"  "V7"  "V8"  "V9"  "V10"
(new_names <- sub("^V", "W", cols_to_process))
#>  [1] "W1"  "W2"  "W3"  "W4"  "W5"  "W6"  "W7"  "W8"  "W9"  "W10"
for(i in seq_along(cols_to_process)){
  medie_i <- data_new2$medie[[i]]
  MT_i <- data_new2$MT[[i]]
  Buc_i<-data_new2$Buc[[i]]
  data_new2[[ new_names[[ i ]] ]] <-floor((medie_i*1000-200)/ data_new2[[ cols_to_process[[ i ]] ]])
  
 
}
head(data_new2)
#>   Buc      MT   Kg Lung medie Pcs      CTT     util  V1  V2  V3  V4  V5  V6  V7
#> 1 302 1884.48 2619  184  6.24  32 1.059783 1778.176 216 213 216 213 216 216 184
#> 2 296 1847.40 2568  216  6.24  27 1.070167 1726.272 216 184 213 184 184 184 216
#> 3 304 1869.96 2599  216  6.15  27 1.054730 1772.928 216 184 213 184 216 184 213
#> 4 240 1497.60 2082  184  6.24  32 1.059783 1413.120 216 213 184 216 216 184 216
#> 5 385 2194.50 3050  216  5.70  25 1.055556 2079.000 213 213 216 216 213 216 213
#> 6 302 1887.50 2624  184  6.25  32 1.061481 1778.176 213 213 213 184 213 216 213
#>    V8  V9 V10 W1 W2 W3 W4 W5 W6 W7 W8 W9 W10
#> 1 184 184 216 27 28 27 28 25 28 32 32 32  28
#> 2 213 216 213 27 32 27 32 29 32 28 28 27  28
#> 3 213 184 184 27 32 27 32 25 32 28 28 32  33
#> 4 216 213 216 27 28 32 27 25 32 28 28 27  28
#> 5 216 216 216 28 28 27 27 25 28 28 28 27  28
#> 6 213 184 213 28 28 27 32 25 28 28 28 32  28
(colnames <- colnames(data_new2))
#>  [1] "Buc"   "MT"    "Kg"    "Lung"  "medie" "Pcs"   "CTT"   "util"  "V1"   
#> [10] "V2"    "V3"    "V4"    "V5"    "V6"    "V7"    "V8"    "V9"    "V10"  
#> [19] "W1"    "W2"    "W3"    "W4"    "W5"    "W6"    "W7"    "W8"    "W9"   
#> [28] "W10"


(cols_to_process <-colnames( data_new2 %>% select(V1:W10)))
#>  [1] "V1"  "V2"  "V3"  "V4"  "V5"  "V6"  "V7"  "V8"  "V9"  "V10" "W1"  "W2" 
#> [13] "W3"  "W4"  "W5"  "W6"  "W7"  "W8"  "W9"  "W10"
(new_names <- sub("^W", "x", cols_to_process))
#>  [1] "V1"  "V2"  "V3"  "V4"  "V5"  "V6"  "V7"  "V8"  "V9"  "V10" "x1"  "x2" 
#> [13] "x3"  "x4"  "x5"  "x6"  "x7"  "x8"  "x9"  "x10"
for(i in seq_along(cols_to_process)){
  V_i<-data_new2$V[[i]]
    W_i<-data_new2$W[[i]]
  Buc_i<-data_new2$Buc[[i]]
    data_new2[[ new_names[[ i ]] ]] <-Buc_i *data_new2[[ cols_to_process[[ i ]] ]]
  
 
}
head(data_new2)
#>   Buc      MT   Kg Lung medie Pcs      CTT     util    V1    V2    V3    V4
#> 1 302 1884.48 2619  184  6.24  32 1.059783 1778.176 65232 63048 65664 51120
#> 2 296 1847.40 2568  216  6.24  27 1.070167 1726.272 65232 54464 64752 44160
#> 3 304 1869.96 2599  216  6.15  27 1.054730 1772.928 65232 54464 64752 44160
#> 4 240 1497.60 2082  184  6.24  32 1.059783 1413.120 65232 63048 55936 51840
#> 5 385 2194.50 3050  216  5.70  25 1.055556 2079.000 64326 63048 65664 51840
#> 6 302 1887.50 2624  184  6.25  32 1.061481 1778.176 64326 63048 64752 44160
#>      V5    V6    V7    V8    V9   V10 W1 W2 W3 W4 W5 W6 W7 W8 W9 W10   x1    x2
#> 1 83160 65232 59248 56304 65688 63720 27 28 27 28 25 28 32 32 32  28 8640  8820
#> 2 70840 55568 69552 65178 77112 62835 27 32 27 32 29 32 28 28 27  28 8640 10080
#> 3 83160 55568 68586 65178 65688 54280 27 32 27 32 25 32 28 28 32  33 8640 10080
#> 4 83160 55568 69552 66096 76041 63720 27 28 32 27 25 32 28 28 27  28 8640  8820
#> 5 82005 65232 68586 66096 77112 63720 28 28 27 27 25 28 28 28 27  28 8960  8820
#> 6 82005 65232 68586 65178 65688 62835 28 28 27 32 25 28 28 28 32  28 8960  8820
#>     x3   x4   x5    x6   x7   x8   x9  x10
#> 1 5886 7560 7000 10080 9216 9408 9856 7924
#> 2 5886 8640 8120 11520 8064 8232 8316 7924
#> 3 5886 8640 7000 11520 8064 8232 9856 9339
#> 4 6976 7290 7000 11520 8064 8232 8316 7924
#> 5 5886 7290 7000 10080 8064 8232 8316 7924
#> 6 5886 8640 7000 10080 8064 8232 9856 7924
#############

Created on 2024-04-06 with reprex v2.0.2

Looks like you're getting the hang of reprex! Just make sure to use the reprex output as-is, and not add anything, like the ```r that I quoted.

From your reprex, I can't tell if you have a complete solution, or if you still have a question — do you?

My solution modified the values form columns V1 to V10 ( i dont want these).At the end ;x(1) should by equal with Buc (1)V(1)w(1) .Whith numbes on first row ;x1=30221627=1761264‬, on second row x1=29621627=1702296‬ .....on first row ;x2=30221328=1801128, on second row x2=296184232 and so on ...

1 Like