This is not so much an issue with transform
as much as it is with data.frame
s, where stringsAsFactors
is set, by default, to TRUE
. Add an argument that it should be FALSE
and you'll be on your way:
y <- transform(x, Alphanumeric = paste(Letters, Numbers),
stringsAsFactors = FALSE)
str(y)
# 'data.frame': 3 obs. of 3 variables:
# $ Letters : Factor w/ 3 levels "A","B","C": 1 2 3
# $ Numbers : int 1 2 3
# $ Alphanumeric: chr "A 1" "B 2" "C 3"
I generally use within
instead of transform
, and it seems to not have this problem:
y <- within(x, {
Alphanumeric = paste(Letters, Numbers)
})
str(y)
# 'data.frame': 3 obs. of 3 variables:
# $ Letters : Factor w/ 3 levels "A","B","C": 1 2 3
# $ Numbers : int 1 2 3
# $ Alphanumeric: chr "A 1" "B 2" "C 3"
This is because it takes an approach similar to your with
approach: Create a character vector and add it (via [<-
) into the existing data.frame
.
You can view the source of each of these by typing transform.data.frame
and within.data.frame
at the prompt.
As for other pitfalls, that's much too broad of a question. One thing that comes to mind right waya is that apply
would create a matrix
from a data.frame
, so all the columns would be coerced to a single type.