0

i want to substract and square the difference of two columns in dataframe by creating a new variable.

IMDB1 imdbVotes imdbRating 45 2700 56 750 N/A 800 67 1400 N/A 850

when i use this code

sub=IMDB$IMDBvote-IMDB$IMDBrating

i get

In Ops.factor(IMDB1$imdbRating, IMDB1$imdbVotes) : ‘-’ not meaningful for factors

.then later i used

votes=as.numeric(IMDB1$imdbVotes)
rating=as.numeric(IMDB1$imdbRating)

and checked still im getting

‘-’ not meaningful for factors.

please help me to get the square of difference of imdbrating and imdbvoting by creating new varibale

kazi
  • 1
  • 3
  • 1
    You have to first convert the factors into character before converting into numeric – Koundy Feb 03 '17 at 09:58
  • can you plz give the code how to convert into character and later into numeric – kazi Feb 03 '17 at 09:59
  • i converted it into character and later into numeric votes=as.character(IMDB1$imdbVotes) rating=as.character(IMDB1$imdbRating)votes=as.numeric(IMDB1$imdbVotes) rating=as.numeric(IMDB1$imdbRating) ,then i applied sq=IMDB1$imdbRating-IMDB1$imdbVotes, still im getting In Ops.factor(IMDB1$imdbRating, IMDB1$imdbVotes) : ‘-’ not meaningful for factors – kazi Feb 03 '17 at 10:01
  • 1
    Errors are very clear. You cannot subtract factors – Sotos Feb 03 '17 at 10:02
  • Please make your example reproducable! Read [mcve] ... then edit your question! http://stackoverflow.com/posts/42021453/edit – jogo Feb 03 '17 at 10:10

1 Answers1

0

Try

IMDB$IMDBvote <- as.numeric(as.character(IMDB$IMDBvote))
IMDB$IMDBrating <- as.numeric(as.character(IMDB$IMDBrating))

Then

sub=IMDB$IMDBvote-IMDB$IMDBrating
Koundy
  • 5,265
  • 3
  • 24
  • 37
  • i used above code im getting- NAs introduced by coercion – kazi Feb 03 '17 at 10:08
  • Probably your data has strings or characters in between. Its not clean enough. These are simple errors and I suggest you to learn basics of R first before diving into these kind of things. – Koundy Feb 03 '17 at 10:11
  • ya some of my data is blank i.e N/A. how to perform code now on unavailable data i,e substracting – kazi Feb 03 '17 at 10:21