x <- data.frame(Level=c(6.5))
x_poly <- poly(x$Level, 4, raw=TRUE)
x1 <- cbind(x, x_poly[, 2:4])
This is my code and whenever I was using cbind for some reason it was printing 3 rows and column even though both x and x_poly have only 1 row.
x_poly <- as.data.frame(x_poly[, 2:4])
print(dim(x_poly))
I tried to print the dimensions of x_poly and x_poly[,2:4]. For some reason x_poly shows 1 row on;y but x_poly[2:4] shows 3 rows.
When extracting using `[`
, R uses drop
(see ?drop
), to coerce to the lowest possible dimensions.
So from the matrix,
> dim(x_poly)
[1] 1 4
you will get a vector.
> dim(x_poly[, 2:4])
NULL
We can do drop=FALSE
.
> dim(x_poly[, 2:4, drop=FALSE])
[1] 1 3
Thus, what you're looking for is
> cbind(x, x_poly[, 2:4, drop=FALSE])
Level 2 3 4
1 6.5 42.25 274.625 1785.062
Data:
x <- data.frame(Level=c(6.5))
x_poly <- poly(x$Level, 4, raw=TRUE)