Skip to content
Back to formatted view

Raw Message

Message-ID: <6DF57D41-2621-4B37-AC0E-DC1325EAD172@gmail.com>
Date: 2011-11-02T16:12:17Z
From: Rebecca Hiller
Subject: difference between foo$a[2] <- 1 and foo[2,"a"] <- 1

Hallo 

Can anyone tell me the difference between

foo$a[2] <- 1 and foo[2,"a"] <- 1 ?

I thought that both expressions are equivalent, but when I run the following example, there is obviously a difference.


> foo <- data.frame(a=NA,b=NA)
> foo
   a  b
1 NA NA
> foo$a[1] <- 1
> foo$b[1] <- 2
> foo$a[2] <- 1
Error in `$<-.data.frame`(`*tmp*`, "a", value = c(1, 1)) : 
  replacement has 2 rows, data has 1
> foo[2,"a"] <- 1
> foo
  a  b
1 1  2
2 1 NA

Thanks,
Rebecca Hiller


--
ETH Z?rich
Rebecca Hiller
Institute of Agricultural Sciences
LFW A2
Universit?tsstrasse 2
8092 Z?rich
SWITZERLAND

rebecca.hiller at ipw.agrl.ethz.ch
http://www.gl.ethz.ch/

+41 44 632 31 90 Telefon
+41 44 632 11 53 Fax