Hello again,
I generally use Clipboard to load data into R from Excel using the
read.delim() function. In most cases, my data after loading looks like
below:
myData <- structure(list(V1 = structure(c(3L, 2L, 1L, 1L, 2L), .Label =
c("",
"2", "a"), class = "factor"), V2 = structure(c(4L,
3L, 1L, 2L,
1L), .Label = c("", "34", "4", "b"),
class = "factor"), V3 = structure(c(3L,
1L, 1L, 1L, 2L), .Label = c("", "4", "c"), class =
"factor"),
V4 = structure(c(3L, 1L, 1L, 2L, 1L), .Label = c("",
"4",
"d"), class = "factor"), V5 = structure(c(4L, 3L, 1L,
1L,
2L), .Label = c("", "4", "7", "f"),
class = "factor")), .Names = c("V1",
"V2", "V3", "V4", "V5"), class =
"data.frame", row.names = c(NA,
-5L))
> myData
V1 V2 V3 V4 V5
1 a b c d f
2 2 4 7
3
4 34 4
5 2 4 4
Now I want to put this data in a Matrix class with all elements are
character. So tried this :
> as.character(myData)
[1] "c(3, 2, 1, 1, 2)" "c(4, 3, 1, 2, 1)" "c(3, 1, 1,
1, 2)" "c(3, 1,
1, 2, 1)" "c(4, 3, 1, 1, 2)"
This looks funny and does not conform the original data. Can somebody
point me what would be right way to put it into Matrix?
Thanks and regards,
Hi,
myData[]<-lapply(myData,as.character)
?as.matrix(myData)
#???? V1? V2?? V3? V4? V5
#[1,] "a" "b"? "c" "d" "f"
#[2,] "2" "4"? ""? ""? "7"
#[3,] ""? ""?? ""? ""? ""
#[4,] ""? "34" ""? "4" ""
#[5,] "2" ""?? "4" ""? "4"
str(as.matrix(myData))
# chr [1:5, 1:5] "a" "2" "" ""
"2" "b" "4" "" "34"
"" ...
?#- attr(*, "dimnames")=List of 2
? #..$ : NULL
? #..$ : chr [1:5] "V1" "V2" "V3" "V4"
...
A.K.
----- Original Message -----
From: Christofer Bogaso <bogaso.christofer at gmail.com>
To: r-help <r-help at r-project.org>
Cc:
Sent: Saturday, January 26, 2013 3:00 PM
Subject: [R] Loading data into R
Hello again,
I generally use Clipboard to load data into R from Excel using the
read.delim() function. In most cases, my data after loading looks like
below:
myData <- structure(list(V1 = structure(c(3L, 2L, 1L, 1L, 2L), .Label =
c("",
"2", "a"), class = "factor"), V2 = structure(c(4L,
3L, 1L, 2L,
1L), .Label = c("", "34", "4", "b"),
class = "factor"), V3 = structure(c(3L,
1L, 1L, 1L, 2L), .Label = c("", "4", "c"), class =
"factor"),
? ? V4 = structure(c(3L, 1L, 1L, 2L, 1L), .Label = c("",
"4",
? ? "d"), class = "factor"), V5 = structure(c(4L, 3L, 1L,
1L,
? ? 2L), .Label = c("", "4", "7", "f"),
class = "factor")), .Names = c("V1",
"V2", "V3", "V4", "V5"), class =
"data.frame", row.names = c(NA,
-5L))
> myData
? V1 V2 V3 V4 V5
1? a? b? c? d? f
2? 2? 4? ? ? ? 7
3
4? ? 34? ? 4
5? 2? ? 4? ? 4
Now I want to put this data in a Matrix class with all elements are
character. So tried this :
> as.character(myData)
[1] "c(3, 2, 1, 1, 2)" "c(4, 3, 1, 2, 1)" "c(3, 1, 1,
1, 2)" "c(3, 1,
1, 2, 1)" "c(4, 3, 1, 1, 2)"
This looks funny and does not conform the original data. Can somebody
point me what would be right way to put it into Matrix?
Thanks and regards,
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
On 13-01-26 3:00 PM, Christofer Bogaso wrote:> Hello again, > > I generally use Clipboard to load data into R from Excel using the > read.delim() function. In most cases, my data after loading looks like > below: > > myData <- structure(list(V1 = structure(c(3L, 2L, 1L, 1L, 2L), .Label = c("", > "2", "a"), class = "factor"), V2 = structure(c(4L, 3L, 1L, 2L, > 1L), .Label = c("", "34", "4", "b"), class = "factor"), V3 = structure(c(3L, > 1L, 1L, 1L, 2L), .Label = c("", "4", "c"), class = "factor"), > V4 = structure(c(3L, 1L, 1L, 2L, 1L), .Label = c("", "4", > "d"), class = "factor"), V5 = structure(c(4L, 3L, 1L, 1L, > 2L), .Label = c("", "4", "7", "f"), class = "factor")), .Names = c("V1", > "V2", "V3", "V4", "V5"), class = "data.frame", row.names = c(NA, > -5L)) > > >> myData > V1 V2 V3 V4 V5 > 1 a b c d f > 2 2 4 7 > 3 > 4 34 4 > 5 2 4 4 > > > Now I want to put this data in a Matrix class with all elements are > character. So tried this : > >> as.character(myData) > [1] "c(3, 2, 1, 1, 2)" "c(4, 3, 1, 2, 1)" "c(3, 1, 1, 1, 2)" "c(3, 1, > 1, 2, 1)" "c(4, 3, 1, 1, 2)" > > > This looks funny and does not conform the original data. Can somebody > point me what would be right way to put it into Matrix?Use as.matrix(myData). Duncan Murdoch