I have the following to create a data frame. My problem is that the data frame row names display as [1,],[2,],[3,],[4,], etc instead of just 1,2,3,4,etc (see below for outputs). Why and how can I fix this to just have my row names show as usual (incremented number).
Code:
df <- data.frame(id=c("id"),nobs=c("nobs"))
df <- cbind(id,nobs)
df
id and nobs are two vectors with 5 numeric values each…
Current output:
id nobs
[1,] 2 3653
[2,] 4 3653
[3,] 8 1462
[4,] 10 1097
[5,] 12 732
Desired output:
id nobs
1 2 3653
2 4 3653
3 8 1462
4 10 1097
5 12 732
Note: I don’t suggest that you should use either of these methods. If you want a
data.frame, you can create it directly as Paul has showed (or you yourself have done). This is just to illustrate why this happens. Although sometimes when usingsapplyfor example, the output would be a matrix and you might want to bind 2 such matrices and have them as adata.frame.From the help page for
cbind(?cbind):So, if you pass two vectors, the wrapper for
data.framewon’t be called. Hence, you obtain a matrix.