An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090223/e314142c/attachment-0001.pl>
array manipulation simple questions
4 messages · Λεωνίδας Μπαντής, jdeisenberg, Gabor Grothendieck +1 more
???????? ??????? wrote:
1. Suppose I have a<-c(1:10) (or a<-array(c(1:10),dim=c(1,10))) and I want to end up with vector b=[0 0 0 0 0 1 1 1 1 1]. i.e. I want to substitute alla elements that are <5 with 0 and >5 with 1.
I think you mean >=5, not > 5. In that case, try this: b <- ifelse( a < 5, 0, 1)
???????? ??????? wrote:
2. Suppose I have a<-c(1,1,2,2,3,3) (or array again) And I want to place a "4,5" before every "2" and end up with a new "bigger" vector "b": b=[1 1 4 5 2 4 5 2 3 3] Also I want to find where the 2's in array "a" (if it was an array) are located i.e. positions 3,4.
Not sure how to insert the 4,5, but this will find the where the 2's are: which(a == 2)
View this message in context: http://www.nabble.com/array-manipulation-simple-questions-tp22173710p22175864.html Sent from the R help mailing list archive at Nabble.com.
2009/2/23 ???????? ??????? <bleonidas25 at yahoo.gr>:
Hi there, I am pretty new to R. Actually I started using it yesterday. I have?two questions: 1.?? Suppose I have a<-c(1:10)? (or a<-array(c(1:10),dim=c(1,10))) and I want to end up with vector b=[0 0 0 0 0 1 1 1 1 1]. i.e. I want to substitute alla elements that are <5 with 0 and >5 with 1. I did this by using a for loop. Is there another function to use so as to avoid the "for"?
(a>5)+0 # or out <- as.numeric(a > 5) if (is.matrix(a)) out <- matrix(a, 1) out
2.? Suppose I have a<-c(1,1,2,2,3,3)? (or array again) And I want to place a "4,5" before every "2" and end up with a new "bigger" vector "b": b=[1 1 4 5 2 4 5 2 3 3]
# return c(4, 5, 2) or x f <- function(x) if (x==2) c(4:5, x) else x # apply f to each element and # if a is a matrix make output a matrix too out <- unlist(sapply(a, f)) if (is.matrix(a)) out <- matrix(out, nrow(a)) out
Also I want to find where the 2's in?array "a"?(if it was an array) are located i.e. positions 3,4.
which(a == 2)
On Feb 24, 3:57?pm, jdeisenberg <catc... at catcode.com> wrote:
???????? ??????? wrote:
1. ? Suppose I have a<-c(1:10) ?(or a<-array(c(1:10),dim=c(1,10)))
and I want to end up with vector b=[0 0 0 0 0 1 1 1 1 1]. i.e. I want to substitute alla elements that are <5 with 0 and >5 with 1.
I think you mean >=5, not > 5. ?In that case, try this: b <- ifelse( a < 5, 0, 1)
you could also use b <- 1*(x >= 5) #the logical elements get converted to numeric
???????? ??????? wrote:
2. ?Suppose I have a<-c(1,1,2,2,3,3) ?(or array again)
And I want to place a "4,5" before every "2" and end up with a new "bigger" vector "b":
b=[1 1 4 5 2 4 5 2 3 3]
Also I want to find where the 2's in array "a" (if it was an array) are located i.e. positions 3,4.
Not sure how to insert the 4,5, but this will find the where the 2's are: which(a == 2)
you could use b <- rep(a,times = 2*(a==2) + 1) replace(b, which(b==2), c(4,5,2))
-- View this message in context:http://www.nabble.com/array-manipulation-simple-questions-tp22173710p... Sent from the R help mailing list archive at Nabble.com.
______________________________________________ R-h... at r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.