[R] Adding new column to data frame and filling some rows of it - classes
Gorjanc Gregor
Gregor.Gorjanc at bfro.uni-lj.si
Thu Feb 17 01:55:45 CET 2005
Hello!
Few days ago I was asking on a list about adding a column and filling
it in some rows. I was satisfied, but one thing raised my attention. I
will show itthrough an example:
tmp <- data.frame(y1=1:4, f1=factor(c("A", "B", "C", "D")))
f2 <- factor(c("Z", "Y"))
# I would like to add f2 to tmp and I know that values in f2 fit to
# first two values in tmp. I used
tmp$f2 <- NA
tmp[1:2, "f2"] <- f2
tmp
y1 f1 f2
1 1 A 2
2 2 B 1
3 3 C NA
4 4 D NA
# However tmp$f2 is not factor. How can I make it to be a factor? I tried
# with
class(tmp$f2) <- "factor"
# but I get this
tmp
y1 f1 f2
1 1 A NULL
2 2 B <NA>
3 3 C <NA>
4 4 D <NA>
Warning message:
corrupt data frame: columns will be truncated or padded with NAs in: format.data.frame(x, digits = digits)
# I tried the other approach
tmp <- data.frame(y1=1:4, f1=factor(c("A", "B", "C", "D")))
f2 <- factor(c("Z", "Y"))
tmp$f2 <- f2
tmp[3:4, "f2"] <- NA
tmp
y1 f1 f2
1 1 A Z
2 2 B Y
3 3 C <NA>
4 4 D <NA>
class(tmp$f2)
[1] "factor"
# And I have now the same class for tmp$f2 as in f2. Is this the
# only way?
--
Lep pozdrav / With regards,
Gregor GORJANC
---------------------------------------------------------------
University of Ljubljana
Biotechnical Faculty URI: http://www.bfro.uni-lj.si
Zootechnical Department email: gregor.gorjanc <at> bfro.uni-lj.si
Groblje 3 tel: +386 (0)1 72 17 861
SI-1230 Domzale fax: +386 (0)1 72 17 888
Slovenia
More information about the R-help
mailing list