transform() . transform() , , . . :
set.seed(42)
df <- data.frame("first" = sample(1:5, 10e5, TRUE), "second" = sample(4:8, 10e5, TRUE))
df <- transform(df
, test = ifelse(first %in% 1:3 & second == 4, 1
, ifelse(first %in% 1:3 & second == 5, 2
, ifelse(first %in% 1:3 & second == 6, 3, NA)))
)
Second, the column names 1stand 2ndare not syntactically valid column names. Take a look at make.names()for more details on what the correct column names are. When working with, data.frameyou can use / abuse the argument check.names. For instance:
> df <- data.frame("1st" = sample(1:5, 10e5, TRUE), "2nd" = sample(4:8, 10e5, TRUE), check.names = FALSE)
> colnames(df)
[1] "1st" "2nd"
> df <- data.frame("1st" = sample(1:5, 10e5, TRUE), "2nd" = sample(4:8, 10e5, TRUE), check.names = TRUE)
> colnames(df)
[1] "X1st" "X2nd"
Chase source
share