numeric vector gets incorrectly converted to characters

Hi. Why did my numeric vector y get converted to characters when I cbinded it to a data frame? Thank you.

x <- c("Al", "Betty", "Chris", "Diane")
y <- c(1,1,0,1)
str(y)
df <- data.frame(cbind(x, y), stringsAsFactors=FALSE)
df
str(df)
sum(df$y)

num [1:4] 1 1 0 1
x y
1 Al 1
2 Betty 1
3 Chris 0
4 Diane 1
'data.frame': 4 obs. of 2 variables:
x: chr "Al" "Betty" "Chris" "Diane" y: chr "1" "1" "0" "1"
Error in sum(df$y) : invalid 'type' (character) of argument
Execution halted

A quick Google search found this from 2012:

cbind() returns a matrix and matrices can have only one data type

Thanks, EconProf.

But does data.frame(cbind(... convert the matrix to a data frame?

You technically never had a matrix. You only had two separate vectors.

For your problem I would just do the following as it creates the dataframe while preserving the state of both to the expected.

x <- c("Al", "Betty", "Chris", "Diane")
y <- c(1,1,0,1)
str(y)
#>  num [1:4] 1 1 0 1


df <- data.frame(x,y)


str(df)
#> 'data.frame':    4 obs. of  2 variables:
#>  $ x: chr  "Al" "Betty" "Chris" "Diane"
#>  $ y: num  1 1 0 1
sum(df$y)
#> [1] 3

Created on 2020-10-10 by the reprex package (v0.3.0)

If you want to convert a matrix to data.frame I would recommend as.data.frame.

Yes, it converts the matrix inside data.frame() to a data frame, but creating that matrix using cbind() converts the second column (y) to the same data type as the first column (x). Each step is completed before it is passed to the next step.

Ah, well done EconProf and GreyMerchant. Thank you.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.