thocar at free.fr
2012-Nov-07 22:20 UTC
[R] Newbie question : selecting subset of a Matrix
I have a matrix, I would like to use it like I use where query on an SQL table Let's say my matrix is> wokwho task 1 joe task1 2 joe task2 3 joe task3 4 jack task1 5 jack task2 I want to extract a matrix that contains only rows where who = "joe" and task != "task2" The result would be the following matrix 1 joe task1 2 joe task3 How can I do that with R ?
Hi, Try this: wok<-read.table(text=" ? who? task ? joe task1 ? joe task2 ? joe task3 ? jack task1 ? jack task2 ",sep="",header=TRUE,stringsAsFactors=FALSE) wok2<- wok[wok[,1]=="joe" & wok[,2]!="task2",] wok2 #? who? task #1 joe task1 #3 joe task3 A.K. ----- Original Message ----- From: "thocar at free.fr" <thocar at free.fr> To: r-help at r-project.org Cc: Sent: Wednesday, November 7, 2012 5:20 PM Subject: [R] Newbie question : selecting subset of a Matrix I have a matrix, I would like to use it like I use where query on an SQL table Let's say my matrix is> wok? ? who? task 1? joe task1 2? joe task2 3? joe task3 4? jack task1 5? jack task2 I want to extract a matrix that contains only rows where ? ? who = "joe" and task != "task2" The result would be the following matrix 1? joe task1 2? joe task3 How can I do that with R ? ______________________________________________ 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.