0

new to stackoverflow and beginner with R.

We have a small competition with my friends regarding upcoming Rio olympics. Everyone is to pick five countries which have points assigned to them (fantasy leagueish), and after competition the winner is the person with most gold medals in his team. I have the perfect opportunity to apply R in a real world situation. I imported the following .xls:

> olympics
      Cost               Country X2012 X2008 X2004  Mean P.Mean   MGold.P  P.MGold.P
1       40   United States (USA)    46    36    36 39.33  41.00     0.98       1.03
2       30           China (CHN)    38    51    32 40.33  41.65     1.34       1.39
...
Totaling 36 rows of data.

What we have here is a simple knapsack problem, which is fairly easy to solve with knapsack(). The problem is, knapsack() doesn't understand my input for some reason:

> library(adagio)
> p <- c(olympics[1])
> w <- c(olympics[7])
> cap <- 50
> myteam <- knapsack(w, p, cap)
Error in numeric(w[k]) : invalid 'length' argument

I'm a little lost why this happens, since I've used the same function before without problems. Here's the contents of w:

> w
$P.Mean
[1] 41.00 41.65 23.25 22.50 13.05 11.45  9.05  9.60 12.40  3.30  3.25  4.50  6.05  4.55  2.50  6.65  6.25  3.70  8.30  4.35  3.20  2.00
[23]  2.65  4.40  1.65  1.80  0.50  2.00  2.70  0.50  0.85  1.50  0.00  0.00  0.00  0.00
> length(w)
[1] 1

I assume the problem is that w is a list and not a numeric, or am I missing something very fundamental here? Also, I appreciate any help how to convert w to a form which is desired in this case.

EDIT: And follow-up, is there a way to convert columns straight to numeric when importing, or are they set to lists by default?

Imrtl
  • 21
  • 3
  • Try `olympics[,7]`? – Xiongbing Jin Jul 18 '16 at 19:17
  • For specifying data types when using `read.table` see the `colClasses` argument. Most other tools for reading in files from other packages have similar functionality, which is easily found if you read the documentation. – joran Jul 18 '16 at 19:23

0 Answers0