Home > Blockchain >  Adding new column with data value in R
Adding new column with data value in R

Time:05-29

forest area to the I want to add a column name (say ForestAreaPerPopn) to find the ratio of forest area to the population(represented by variable Total below) residing. The data contains the following variables and their values. enter image description here

How can I add a column named ForestAreaPerPopn in Table****ForestAreaPerPop (shown below) so that the column contains the data calculated as ratio of forest area to Total.

CodePudding user response:

Too long for a comment.

You have a couple of problems. First, your column names have spaces and other special characters. This is allowed but creates all kinds of problems later. I suggest you do something like:

colnames(ForestAreaPerPop) <- gsub(' |\\(|\\)', '_', colnames(ForestAreaPerPop))

This will replaces any spaces, left or right parens in the colnames with '_'.

Then, something like:

ForestAreaPerPop$n <- with(ForestAreaPerPop, Forest_Area_in_ha/Total)

should give you what you want.

Some advice: long table names and column names may seem like a good idea, but you will live to regret it. Make them short but meaningful (easier said than done).

  • Related