0

I managed to remove the first row of the matrix from what I learned from digging around this website, but apparently there's an easier way to do it. This is what I currently have:

> prestige.income <- lm(prestige ~ log2(income), data=Prestige)
> prestige.edu <- lm(prestige ~ education, data=Prestige)
> prestige.women <- lm(prestige ~ women, data=Prestige)
> prestige.all <- lm(prestige ~ education + income + women, data=Prestige) 
> comparison <- cbind(INCOME=coef(prestige.income), EDUCATION=coef(prestige.edu),
  WOMEN=coef(prestige.women), ALL=coef(prestige.mod))
> comparison
                 INCOME  EDUCATION       WOMEN           ALL
(Intercept)  -139.85572 -10.731982 48.69299929 -110.96582409
education      14.94173   5.360878 -0.06417284    3.73050783
log2(income) -139.85572 -10.731982 48.69299929    9.31466643
women          14.94173   5.360878 -0.06417284    0.04689514
> remove <- rownames(comparison)[1]
> remove
[1] "(Intercept)"
> comp.noint <- comparison[!rownames(comparison) %in% remove, ]
> comp.noint
             INCOME  EDUCATION       WOMEN        ALL
education      14.94173   5.360878 -0.06417284 3.73050783
log2(income) -139.85572 -10.731982 48.69299929 9.31466643
women          14.94173   5.360878 -0.06417284 0.04689514

This is supposedly the easier way/way I was supposed to do it, but I don't understand why it works:

> comparison[-1,-5]
             INCOME  EDUCATION       WOMEN        ALL
education      14.94173   5.360878 -0.06417284 3.73050783
log2(income) -139.85572 -10.731982 48.69299929 9.31466643
women          14.94173   5.360878 -0.06417284 0.04689514

I was told the -1 gets rid of the first row, but it doesn't actually do that..

> comparison[-1]
 [1]   14.94173191 -139.85572422   14.94173191  -10.73198197    5.36087773
 [6]  -10.73198197    5.36087773   48.69299929   -0.06417284   48.69299929
[11]   -0.06417284 -110.96582409    3.73050783    9.31466643    0.04689514

And I was given no explanation for why the -5 does anything. I know this is probably a really basic question, but my teacher couldn't explain to me why this works and I can't find anything on Google or in the textbooks. Any help would be great. Thanks!

1 Answers1

1

Using comparison[-1] with a single argument for [], you no longer have a matrix, but a vector of length 16 (4 x 4), formed by all columns of comparison concatenated one after the other. A negative argument drops the first element (-139.85572) and you are left with the remaining 15. Note that they are printed left to right, not top to bottom like columns of a matrix.

Using comparison[-1,-5] you still have a matrix, where the first row and fifth column are droped. Oh, wait, there is no fifth column, so -5 is ignored. Try comparison[-1,-4] for instance.

Ferdinand.kraft
  • 12,579
  • 10
  • 47
  • 69
  • So the first term in comparison[x,y] is for the row, and the second term is for the column? That makes sense. Is there an easier way to do this or is this the recommended way? – Lucky Observer Sep 24 '13 at 01:28
  • @LuckyObserver negative indices are the best way to drop rows, IMHO. And to get a little more advanced, you could use `comparison[x,y,drop=FALSE]`, only to avoid that single-row or single-column results get simplified to a vector. – Ferdinand.kraft Sep 24 '13 at 01:33
  • I see. Thanks! So in what context would you use what I originally did (in the first box)? – Lucky Observer Sep 24 '13 at 01:40
  • @LuckyObserver what do you mean? This: `comparison[!rownames(comparison) %in% remove, ]`? – Ferdinand.kraft Sep 24 '13 at 01:45
  • Yeah, that's what I meant. Sorry for not being specific! I'm not really sure what it does, I found it on this site and it worked so I ended up just using it. xD – Lucky Observer Sep 24 '13 at 02:01
  • @LuckyObserver the example you have is the most simple way to drop a row (or column) by name; it looks complicated because you can't negate a string, you must first match the name you want to drop with the names of your matrix, and then negate the logical result. maybe `comparison[!(rownames(comparison) %in% remove), ]` is more clear. – Ferdinand.kraft Sep 24 '13 at 13:55