Hi, I have a problem sorting a table; When I read a table into R by x <- read.table() I get something like this: V1 V2 V3 yes 1 3 no 2 6 yes 3 9 no 4 12 Now I want to generate a vector of V2. But R should only put in the numbers of V2 into the new vector when there is the entry "yes" in V1. How can I realize that? Thanks for help. I am getting crazy trying to solve that ...
J?rg Gro? wrote:> Hi, > > I have a problem sorting a table; > > When I read a table into R by x <- read.table() I get something like > this: > > V1 V2 V3 > yes 1 3 > no 2 6 > yes 3 9 > no 4 12 > > Now I want to generate a vector of V2. > But R should only put in the numbers of V2 into the new vector when > there is the entry "yes" in V1. > > How can I realize that? >x[x$V1=="yes","V2"] vQ
Type ?ifelse in the R prompt and hit enter you will have to put the yes (or the no) in " " in your ifelse command. Best, Daniel J?rg Gro? wrote:> > Hi, > > I have a problem sorting a table; > > When I read a table into R by x <- read.table() I get something like > this: > > V1 V2 V3 > yes 1 3 > no 2 6 > yes 3 9 > no 4 12 > > Now I want to generate a vector of V2. > But R should only put in the numbers of V2 into the new vector when > there is the entry "yes" in V1. > > How can I realize that? > > Thanks for help. I am getting crazy trying to solve that ... > > ______________________________________________ > 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. > >-- View this message in context: http://www.nabble.com/Sorting---editing-a-table-tp18379965p18380089.html Sent from the R help mailing list archive at Nabble.com.
J?rg Gro? wrote:> I have a problem sorting a table; > > When I read a table into R by x <- read.table() I get something like this: > > V1 V2 V3 > yes 1 3 > no 2 6 > yes 3 9 > no 4 12 > > Now I want to generate a vector of V2. > But R should only put in the numbers of V2 into the new vector when > there is the entry "yes" in V1.x <- data.frame(V1 = factor(c("yes", "no", "yes", "no")), V2 = 1:4) # vector as result newVector <- x$V2[x$V1 == "yes"] newVector # subsetting a data.frame newX <- subset(x, V1 == "yes") newX HTH, Tobias