Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Converting Text to Number and Then Multiplying Fails in R

$
0
0

I have three data frames each with one column and same length with the following example data:

df1 = (1, 2, 3)
df2 = (a, b, a)
df3 = (2, 3, 4)

I convert all "a" values in df2 to 100 and all "b" values in df2 to -100 using the following code:

df2[df2 == "a"] <- as.numeric(100)
df2[df2 == "b"] <- as.numeric(-100)

Then I create df4 by multiplying df1, df2, and df3 using this code:

df4 <- (df1 * df2 * df3)

However, I get the following error:

Error in FUN(left, right) : non-numeric argument to binary operator

What am I doing wrong? I can multiply df1 and df3 without issue, it is only when I add df2 that a problem results. If I look to average df2 I get that the average is "NA_real_", which leads me to believe that my numeric values are not being treated as numbers. Thanks!


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>