I have the following data.frame x
a d g
b e h
c f i
Note: letters are used as an example. The actual data.frame has numbers in place of letters (real data which are not in ascending order, as in this example)
I want to transform it in an unique column, by putting columns from 2 to 4 under the first. The expected result is the following
a
b
c
d
e
f
g
h
i
I tried the following code
matrix(t(x), ncol=1, nrow=ncol(x)*nrow(x), byrow=F)
but it gives (obviously) the following
a
d
g
b
e
h
c
f
i
Rotating or transposing R objects frame so that the rows become the columns and the columns become the rows. That is, you transpose the rows and columns. You simply use the t() command.
Here is how it looks: Select and copy the needed range. Right-click on a cell where you want to convert rows to columns. Select the Paste Transpose option to rotate rows to columns.
Convert multiple columns into a single column, To combine numerous data frame columns into one column, use the union() function from the tidyr package.
To interchange rows with columns, you can use the t() function. For example, if you have the matrix (or dataframe) mat you can transpose it by typing t(mat) . This will, as previously hinted, result in a new matrix that is obtained by exchanging the rows and columns.
Something like this?
X <- matrix(letters[1:9], ncol=3)
matrix(X, ncol=1)
R matrices are in column major order, so you can easily concatenate them into a single column vector with the matrix
function.
I find stack
also very useful
X <- data.frame(matrix(letters[1:9], ncol=3),stringsAsFactors=FALSE)
stack(X)[,"values",drop=FALSE]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With