Reputation: 13
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!
Upvotes: 1
Views: 1209
Reputation: 4472
fulldata$Conpolls <- (fulldata$Conprct * fulldata$Total.seats)/100
This doesn't answer the question, however this should be the proper syntax to write such arithmetic operations. And yes as mentioned in the comments you should check the class of the objects you are using to find out what is wrong
Upvotes: 1
Reputation: 13274
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
Upvotes: 1