Reputation: 37
Collected several questions during the last days of programming. Trying to make a program that make a prognosis on how much money I get after working for 5 years. One problem is when I try to take the money in minus money out.
moneyover.year[i] = moneyin.year[i] - moneyout.year[i]
Then it gives this error: "non-numeric argument to binary operator"
I would like to extract information from each year with the function "summary" but it doesn't really work... It just shows:
Length Class Mode [1,] 100 -none- numeric
Finally I want to predict how many holidays I will get. The graph works but I want to get the percentage sign on y-axis. Preferably a smooth curve with the histogram.
Here is the full code:
library(ggplot2)
library(mc2d)
library(scales)
moneyin.year= NULL # Is there a work around this?
moneyout.year= NULL # Is there a work around this?
moneyover.year = NULL # Is there a work around this?
n=100
for (i in 1:5 ) {
moneyin.year[i] <- list(rpert(n, min=20000, mode=23000, max=30000, shape=30))
moneyout.year[i] <- list(rpert(n, min=10000, mode=12500, max=19500, shape=20))
moneyover.year[i] = moneyin.year[i] - moneyout.year[i] # GIVES ERROR!
}
moneyin.year
moneyout.year
moneyover.year # GIVES ERROR!
#graph = moneyover.year[1]
graph = moneyin.year[1]
summary(graph) # Doesnt really work...
sd(graph) # GIVES ERROR!
var(graph) # GIVES ERROR!
p <- ggplot(data.frame(graph), aes(x = graph))
p <- p + geom_bar(aes(y = (..count..)/sum(..count..)), color="black", binwidth = 500, fill = "steelblue")
p <- p + scale_y_continuous(labels = percent)
p <- p + xlab("EUR") + ylab("Percent")
p <- p + theme_bw()
print(p)
extraholidays = dpois(20:50,30)
barplot(extraholidays,names=20:50,xlab='Days',ylab='Percentage')
# How to get "%" sign???
# How to get a smooth line?
Upvotes: 0
Views: 77
Reputation: 10167
You're getting an error when you call moneyover.year[i] = blah blah blah
because you haven't initialized moneyover.year
and you cant take or assign to the subset of a vector that doesn't yet exist. (you initilized moneyover
and not moneyover.year
...).
Initilization is just a fact of programming, but you can use multiple assignment to initialize several variables at once, as in:
moneyin.year <- moneyout.year <- moneyover.year <- NULL
The second error has to do with R's type conversion. Specifically, in the first iteration of the for loop, this linemoneyin.year[i] <- list(blah blah blah)
coerces moneyin.year
to a list. This is a problem when this line is evaluated:
moneyover.year[i] = moneyin.year[i] - moneyout.year[i]
because the single bracket operator ([
) returns a sub-list, and not the first element of the list. This is a problem because the minus operator (-
) is not defined for lists (hence, the somewhat non-intuitive error "non-numeric argument to binary operator"). Instead, you want to use the double-bracket operator ([[
) which returns the value stored in the i
'th element of the list. as in:
moneyover.year[[i]] = moneyin.year[[i]] - moneyout.year[[i]]
Since all three of the objects above are lists, it might save on confusion to initialize them as lists rather than as NULL
values, as in:
moneyin.year <- moneyout.year <- moneyover.year <- list()
Upvotes: 1