How can R language split a single column of numbers int…
In the R language, you can use the matrix or data.frame functions to split one column of numbers into multiple columns.
Option 1: Utilize the matrix function.
# 创建一列数
x <- c(1, 2, 3, 4, 5, 6)
# 将一列数分成3列
result <- matrix(x, ncol = 3)
print(result)
Output results:
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
Option two: employ the data.frame function.
# 创建一列数
x <- c(1, 2, 3, 4, 5, 6)
# 将一列数分成3列
result <- data.frame(matrix(x, ncol = 3))
print(result)
Output result:
X1 X2 X3
1 1 3 5
2 2 4 6
Both methods divide one column of numbers into three columns, and you can adjust the value of the ncol parameter based on your needs.