R - converting data frame to matrix, get different results when using head() -


i have data frame has around 78,000 rows , looks this:

emailaddress    column1 column2 column3 abc1@gmail.com     1       0       0 abc2@gmail.com     0       0       0 abc3@gmail.com     1       1       0 abc4@gmail.com     1       1       0 

i want run kmeans clustering on using kcca() function. want convert data frame matrix want keep column emailaddress can link output original data. ran as.matrix() function follows , seems different results when run str() command.

first time im running on whole data frame, can see first element, should column names null , second element, should data 1 or 0, emailaddresses.

second time run it, im using head() function, asking top 100,000 , matrix im looking for.

why case?

> y <- as.matrix(clicked_data[,1:24]) > str(y)  chr [1:78748, 1:24] "abc1@gmail.com" "abc2@gmail.com" "abc3@msn.com" ...  - attr(*, "dimnames")=list of 2   ..$ : null   ..$ : chr [1:24] "emailaddress" "column1" "column2" "column3" ...  > y <- as.matrix(head(clicked_data[,1:24],100000)) > str(y)  chr [1:10000, 1:24] "abc1@gmail.com" "abc2@gmail.com" "abc3@msn.com" ...  - attr(*, "dimnames")=list of 2   ..$ : chr [1:78748] "1" "2" "3" "4" ...   ..$ : chr [1:24] "emailaddress" "column1" "column2" "column3" ... 

in first case null because rows of matrix not named. on other hand, head() seems put row names appears in dimnames.

the output clarifies it.

df <- read.table(head = t, text = "emailaddress    column1 column2 column3 abc1@gmail.com     1       0       0 abc2@gmail.com     0       0       0 abc3@gmail.com     1       1       0 abc4@gmail.com     1       1       0")  mat <- as.matrix(df) #         emailaddress     column1 column2 column3 #[1,] "abc1@gmail.com" "1"     "0"     "0"     #[2,] "abc2@gmail.com" "0"     "0"     "0"     #[3,] "abc3@gmail.com" "1"     "1"     "0"     #[4,] "abc4@gmail.com" "1"     "1"     "0"   mat1 <- as.matrix(head(df, 2)) #      emailaddress     column1 column2 column3 #1 "abc1@gmail.com" "1"     "0"     "0"     #2 "abc2@gmail.com" "0"     "0"     "0"   

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -