I have a 3*4 matrix (called S1), let's say:
1 2 3 4
5 6 7 8
9 1 1 1
and I have a character vector called 'frequency':
CX
XU
UK
The idea is to bind them such as
CX 1 2 3 4
XU 5 6 7 8
UK 9 1 1 1
I have tried using cbind as
result = cbind(frequency, S1)
But the result that R produces is
"CX" "1" "2" "3" "4"
"XU" "5" "6" "7" "8"
"UK" "9" "1" "1" "1"
Any idea how to get rid of the parenthesis?
Generate data
frequency <- c("CU", "XU", "UK")
S1 <- matrix(c(1:8,9,1,1,1), nrow = 3, ncol = 4, byrow=TRUE)
One can use this
cbind.data.frame(freq=frequency,s1=S1)
Output
freq s1.1 s1.2 s1.3 s1.4
1 CU 1 2 3 4
2 XU 5 6 7 8
3 UK 9 1 1 1
S1
is matrix and matrix can hold data of only one type. Since frequency
is a character vector it coerces S1
values to character as well.
Turn S1
to dataframe
cbind(frequency, data.frame(S1))
OR
cbind.data.frame(frequency, S1)
A matrix only accepts one type of data, in your case transforming numeric to character. I recomend you to use the data structure of a data frame, the indexing is the same as a matrix. Try:
result = cbind(frequency, as.dataframe(S1))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.