As commented above.. a matrix in R can only hold values of the same type in all columns. You cannot change some of the values to numeric and leave some others as characters. If you want different data types, you can use a data.frame, but even then, you can only have one data type per column.
For your example case:
df2 <- as.data.frame(matrix(as.character(1:9),3,3))
will create a data.frame with factors in each column. If you want to convert the second column to numeric, you can do:
df2$V2 <- as.numeric(levels(df2$V2))[df2$V2]
Or
df$V2 <- as.numeric(as.character(df2$V2))
So you don't need to use apply in this case.
str(df2)
#'data.frame': 3 obs. of 3 variables:
# $ V1: Factor w/ 3 levels "1","2","3": 1 2 3
# $ V2: num 4 5 6
# $ V3: Factor w/ 3 levels "7","8","9": 1 2 3
If you wanted to convert all columns to numeric, you can do:
# if the columns were factors before:
df2[] <- lapply(df2, function(i) as.numeric(levels(i))[i])
Or
# if the columns were characters before:
df2[] <- lapply(df2, as.numeric)
df2 <- data.frame(df2,stringsAsFactors=F)after your first line.