Search code examples
rmultiplication

Multiplying/dividing columns in a data frame in r


I have a dataframe in which I want to add a new column which is the product of two other columns, divided by 100.

The command I'm trying to use is:

fulldata$Conpolls <- fulldata$Conprct/100 * fulldata$Total.seats 

for which I receive:

Error: unexpected input in "full.data$Conpolls <- fulldata$Conprct /100 * fulldata$Total.seats"

When I try to break up the process in 2 steps as:

fulldata$Conpolls <- fulldata$Conprct * fulldata$Total.seats 

I get the error:

non-numeric argument to binary operator.

Any tips or help from experienced users greatly appreciated!


Solution

  • Veerendra Gadekar's answer should be correct if all the columns are numeric values.

    If the columns with which you are doing the operations are not guaranteed to be numeric, you may turn them into numeric values with as.numeric(). It should look like this:

    fulldata$Conpolls <- (as.numeric(fulldata$Conprct) * as.numeric(fulldata$Total.seats))/100