Generating equations from factors in R -
this question has answer here:
i new r, , trying create new column, 1 column minus column. example:
price <- c("$10.00", "$7.15", "$8.75", "12.00", "9.20") quantity <- c(5, 6, 7, 8, 9) price <- as.factor(price) quantity <- as.factor(quantity) df <- data.frame(price, quantity)
in actual data set, columns imported factors. when try create new column this:
diff <- price - quantity in ops.factor(price, quantity): - not meaningful factors
i have tried coerce data numeric using as.numeric(df), as.numeric(levels(df)), as.numeric(levels(df))[df], , setting stringsasfactors false, data gets converted nas. data.matrix changes values. there way above equation work? thanks!
you should avoid "" , $ in price column , avoid converting them factors if want math operations on them:
price <- c(10.00, 7.15, 8.75, 12.00, 9.20) quantity <- c(5, 6, 7, 8, 9) df <- data.frame(price, quantity) df$diff <- price - quantity df price quantity diff 1 10.00 5 5.00 2 7.15 6 1.15 3 8.75 7 1.75 4 12.00 8 4.00 5 9.20 9 0.20
Comments
Post a Comment