[R] Extract row form a dataframe by row names in another vector and factor . Need explanation
Jeff Newmiller
jdnewmil at dcn.davis.ca.us
Thu Mar 3 07:36:51 CET 2016
Posting in HTML makes a mess of your code... learn to post in plain text.
Not sure what you thought you would accomplish by using as.vector... perhaps you should read the help file ?as.vector.
Did you look at str( dat1 )?
Factors are more closely akin to integers than to characters. Indexing with factors is the same as indexing with the integers that factors are implemented with (see the discussion of factors in the Introduction to R document that comes with R) than indexing with character strings. If you want character indexing, use character vectors.
Advice: 98% of the time making a data frame using a matrix causes more damage than help. Data frames are a list of columns, each of which potentially has its own storage mode. Matrices are all one type, implemented as a folded vector. Use named arguments to the data.frame function instead. Also use the stringsAsFactors = FALSE argument to data.frame unless you know you won't want character strings.
--
Sent from my phone. Please excuse my brevity.
On March 2, 2016 6:05:37 PM PST, Mohammad Tanvir Ahamed via R-help <r-help at r-project.org> wrote:
>Hi,Here i have written an example to explain my problem
>## Data Generationdat<-data.frame(matrix(1:50,ncol=5))
>rownames(dat)<-letters[1:10]
>colnames(dat)<- c("SA1","SA2","SA3","SA4","SA5")
>
>dat1<-data.frame(matrix(letters[1:20],ncol=4))
>colnames(dat1)<-c("AA","BB","CC","DD")
>
>## Row names
>v1<-dat1[,"BB"] # Factor
>v2<-as.vector(dat1[,"BB"]) # Vector
>
>is(v1) # Factor
>is(v2) # Vector
>
># Result
>res1<-dat[v1,]
>res2<-dat[v2,]
>##########################################################i assumed
>res1 and res2 are same . but it is not . Can any body please explain
>why ?
>
>
>Tanvir Ahamed
>Göteborg, Sweden | mashranga at yahoo.com
> [[alternative HTML version deleted]]
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>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.
[[alternative HTML version deleted]]
More information about the R-help
mailing list