Columns in data frames must all have the same number of elements. Your first example attempts to violate that, because it works with a single column. The second example works on the entire data frame, so it is able to lengthen the other column to match.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil@dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

Rebecca Hiller <hillerrv@gmail.com> wrote:

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@ipw.agrl.ethz.ch
http://www.gl.ethz.ch/

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

_____________________________________________

R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


	[[alternative HTML version deleted]]

