Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)

I want to add multiple sets of columns (numeric) from tbl.male and tbl.female to tbl.total using for-loop.

The code is:

for ( i in c(4:17){

     tbl.total[,i] <- tbl.male[,i] + tbl.female[,i]

}

Error warning:

Error in `[.data.table`(tbl.female, , i) : 

  j (the 2nd argument inside [...]) is a single symbol but column name 'i' is not found. Perhaps you intended DT[, ..i]. This difference to data.frame is deliberate and explained in FAQ 1.1.

1 Answer

0 votes
by (107k points)

From the error message, I know exactly what was wrong, kindly apply the below code:

   for(i in 4:17) {

      tbl.total <- tbl.male[,..i] + tbl.female[,..i]

    }

If you are a beginner and want to know more about R then do check out the R programming tutorial

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...