[R] Adding new column to data frame and filling some rows of it - classes
Gabor Grothendieck
ggrothendieck at myway.com
Thu Feb 17 02:53:32 CET 2005
Gorjanc Gregor <Gregor.Gorjanc <at> bfro.uni-lj.si> writes:
: 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?
Here are a few possibilities:
R> # 1
R>
R> tmp <- data.frame(y1=1:4, f1=factor(c("A", "B", "C", "D")))
R> f2 <- factor(c("Z", "Y"))
R>
R> length(f2) <- nrow(tmp)
R> tmp$f2 <- f2
R> tmp
y1 f1 f2
1 1 A Z
2 2 B Y
3 3 C <NA>
4 4 D <NA>
R>
R> # 2
R>
R> tmp <- data.frame(y1=1:4, f1=factor(c("A", "B", "C", "D")))
R> f2 <- factor(c("Z", "Y"))
R>
R> tmp$f2 <- factor(NA, levels = levels(f2))
R> tmp[seq(along = f2),"f2"] <- f2
R> tmp
y1 f1 f2
1 1 A Z
2 2 B Y
3 3 C <NA>
4 4 D <NA>
R>
R> # 3
R>
R> tmp <- data.frame(y1=1:4, f1=factor(c("A", "B", "C", "D")))
R> f2 <- factor(c("Z", "Y"))
R>
R> merge(tmp, list(f2 = f2), by = 0, all.x = TRUE)
Row.names y1 f1 f2
1 1 1 A Z
2 2 2 B Y
3 3 3 C <NA>
4 4 4 D <NA>
---------------
Here is just the input:
# 1
tmp <- data.frame(y1=1:4, f1=factor(c("A", "B", "C", "D")))
f2 <- factor(c("Z", "Y"))
length(f2) <- nrow(tmp)
tmp$f2 <- f2
tmp
# 2
tmp <- data.frame(y1=1:4, f1=factor(c("A", "B", "C", "D")))
f2 <- factor(c("Z", "Y"))
tmp$f2 <- factor(NA, levels = levels(f2))
tmp[seq(along = f2),"f2"] <- f2
tmp
# 3
tmp <- data.frame(y1=1:4, f1=factor(c("A", "B", "C", "D")))
f2 <- factor(c("Z", "Y"))
merge(tmp, list(f2 = f2), by = 0, all.x = TRUE)
More information about the R-help
mailing list