Home > OS >  Writing a 'for' loop to create a new column and multiply values based on a specified formu
Writing a 'for' loop to create a new column and multiply values based on a specified formu

Time:02-08

I am new to 'for' loops in R, and I'm trying to create a new column in the dataframe and pass a formula through it, with the output being a calculated value in each cell. Here is the data I am working with:

data <- data.frame(plot1=c(133,
                           134,
                           115,
                           135,
                           119,
                           41,
                           96,
                           147,
                           64,
                           18,
                           123,
                           198,
                           40,
                           142,
                           131,
                           175,
                           52,
                           7,
                           100,
                           30,
                           197,
                           200,
                           45,
                           149,
                           170,
                           87,
                           191,
                           90,
                           50,
                           168,
                           153,
                           70,
                           98,
                           76,
                           12,
                           83,
                           101,
                           26,
                           62,
                           96,
                           52,
                           97,
                           90,
                           65,
                           9,
                           146,
                           182,
                           174,
                           15,
                           78,
                           165,
                           29,
                           186,
                           132,
                           167,
                           6,
                           43,
                           135,
                           148,
                           49,
                           64,
                           102,
                           195,
                           103,
                           105,
                           75,
                           189,
                           28,
                           117,
                           112,
                           187,
                           13,
                           109,
                           123,
                           61,
                           13,
                           160,
                           172,
                           125,
                           34,
                           75,
                           89,
                           19,
                           121,
                           136,
                           3,
                           32,
                           161,
                           111,
                           158,
                           135,
                           78,
                           133,
                           7,
                           182,
                           159,
                           99,
                           155,
                           80,
                           186,
                           120,
                           74,
                           119,
                           105,
                           157,
                           187,
                           46,
                           92,
                           25,
                           48,
                           129,
                           156,
                           180,
                           106,
                           115,
                           188,
                           120,
                           133,
                           61,
                           152,
                           152,
                           31,
                           84,
                           8,
                           20,
                           40,
                           59,
                           136,
                           84,
                           111,
                           167,
                           53,
                           40,
                           121,
                           29,
                           127,
                           135,
                           177,
                           54,
                           103,
                           41,
                           15,
                           21,
                           108,
                           105,
                           139,
                           73,
                           126,
                           59,
                           165,
                           91,
                           56,
                           53,
                           126,
                           26,
                           124,
                           128,
                           86,
                           109,
                           50,
                           40,
                           8,
                           21,
                           177,
                           198,
                           128,
                           160,
                           97,
                           12,
                           128,
                           102,
                           57,
                           197,
                           167,
                           90,
                           33,
                           59,
                           79,
                           167,
                           5,
                           52,
                           58,
                           22,
                           200,
                           38,
                           163,
                           77,
                           188,
                           136,
                           26,
                           10,
                           37,
                           179,
                           98,
                           170,
                           35,
                           30,
                           144,
                           61,
                           58,
                           141,
                           165,
                           37,
                           98,
                           2,
                           103,
                           133,
                           88,
                           172,
                           114,
                           132,
                           89,
                           170,
                           62,
                           12,
                           27),
                   plot2=c(412,
                           303,
                           10,
                           45,
                           161,
                           181,
                           450,
                           647,
                           43,
                           407,
                           170,
                           470,
                           404,
                           505,
                           293,
                           173,
                           217,
                           238,
                           318,
                           443,
                           378,
                           317,
                           29,
                           64,
                           192,
                           249,
                           550,
                           328,
                           605,
                           552,
                           58,
                           226,
                           502,
                           193,
                           506,
                           589,
                           160,
                           261,
                           515,
                           563,
                           550,
                           645,
                           488,
                           547,
                           434,
                           652,
                           70,
                           573,
                           623,
                           351,
                           450,
                           384,
                           82,
                           315,
                           517,
                           626,
                           524,
                           650,
                           466,
                           8,
                           452,
                           624,
                           306,
                           431,
                           460,
                           587,
                           619,
                           139,
                           43,
                           124,
                           38,
                           394,
                           53,
                           436,
                           625,
                           173,
                           509,
                           32,
                           115,
                           156,
                           465,
                           656,
                           584,
                           636,
                           255,
                           212,
                           317,
                           154,
                           153,
                           111,
                           554,
                           653,
                           15,
                           32,
                           118,
                           532,
                           415,
                           615,
                           124,
                           416,
                           546,
                           51,
                           503,
                           448,
                           612,
                           551,
                           286,
                           347,
                           52,
                           435,
                           592,
                           138,
                           182,
                           622,
                           646,
                           5,
                           158,
                           473,
                           640,
                           500,
                           7,
                           578,
                           1,
                           644,
                           230,
                           374,
                           452,
                           307,
                           271,
                           462,
                           647,
                           648,
                           358,
                           262,
                           614,
                           79,
                           278,
                           534,
                           68,
                           359,
                           357,
                           106,
                           589,
                           575,
                           457,
                           555,
                           252,
                           418,
                           371,
                           654,
                           88,
                           609,
                           589,
                           215,
                           435,
                           82,
                           600,
                           266,
                           510,
                           307,
                           537,
                           148,
                           536,
                           290,
                           36,
                           301,
                           27,
                           129,
                           77,
                           595,
                           506,
                           431,
                           293,
                           232,
                           108,
                           246,
                           354,
                           347,
                           460,
                           384,
                           452,
                           261,
                           132,
                           499,
                           463,
                           187,
                           422,
                           319,
                           48,
                           284,
                           241,
                           151,
                           611,
                           496,
                           572,
                           501,
                           644,
                           387,
                           480,
                           221,
                           146,
                           39,
                           400,
                           361,
                           531,
                           69,
                           539,
                           295,
                           249,
                           638,
                           289,
                           83,
                           404,
                           289,
                           547,
                           324))

The new column created is data$newCol, and I'm trying to calculate the area of a circle using the data in data$plot2 and then multiply that value by data$plot1. The result should be in the data$newCol column.

Here is what I have tried:

data$newCol <- for(i in data$newCol) {
  ((3.14(data$plot2^2)) * (data$plot1)
}

However, I keep getting this error:

Error: attempt to apply non-function

Can someone help me properly write this loop?

CodePudding user response:

I see a few issues with what you're doing:

  1. To multiply 3.14 by something you need to supply a multiplication operator *. When you write 3.14(x) that is the construction of a function where 3.14 is the name of the function and x is it's arguments. That's what's causing the error

Error: attempt to apply non-function

  1. If data$newCol doesn't exist yet you can't loop over it.
  2. This doesn't need to be a loop because those mathematical operations are vectorized so they will just act element by element down the vectors supplied if they are the same length.
  3. R has a built in constant for pi so you can get better precision than 3.14 if you're interested.

So with that in mind, try this:

data$newCol <- pi * (data$plot2^2) * data$plot1

CodePudding user response:

If I understood you correctly you are looking for:

data$newCol <- pi*data$plot2^2 * data$plot1

The above would give you a new column. In R, these operations are vectorised, thus, loops are not needed.

  •  Tags:  
  • Related