Re: [R] creating vectors from data-frames
On Jan 8, 2012, at 3:12 PM, Philip Robinson wrote: I am having a problem with creating a vector from a rows or columns, I searched around and found as.vector(x), but it does not seem to do what it says it does I have included an example below, of doing what would seem to be the method required to create a vector, but instead it creates a one row data frame. What is required to actually create a vector. Many thanks Philip data Ugh. Bad name ofr data-object. V1 V2 V3 V4 V5 V6V7V8V9 V10 V11 1 E 2369 2304 2312 2460 2645 3038 3265 3760 3904 4421 2 NZ 705 817 907 917 954 1,026 1,065 1,125 1,276 1,449 The fact that any column has a comma in the output implies that it is a character or a factor column. nz <-as.vector(data[2,2:11]) You could use unlist() or c() but I am guessing that your data entry might have results in strange consequences: data <- read.table(text=" V1 V2 V3 V4 V5 V6V7V8 V9 V10 V11 1 E 2369 2304 2312 2460 2645 3038 3265 3760 3904 4421 2 NZ 705 817 907 917 954 1,026 1,065 1,125 1,276 1,449", header=TRUE) > unlist(data[2,]) V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 2 705 817 907 917 954 1 1 1 1 1 You have not offered dput() on the head of that dataframe. You would be well advised to do so now. nz V2 V3 V4 V5 V6V7V8V9 V10 V11 2 705 817 907 917 954 1,026 1,065 1,125 1,276 1,449 class(nz) [1] "data.frame" -- David Winsemius, MD West Hartford, CT __ R-help@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.
[R] creating vectors from data-frames
I am having a problem with creating a vector from a rows or columns, I searched around and found as.vector(x), but it does not seem to do what it says it does I have included an example below, of doing what would seem to be the method required to create a vector, but instead it creates a one row data frame. What is required to actually create a vector. Many thanks Philip > data V1 V2 V3 V4 V5 V6V7V8V9 V10 V11 1 E 2369 2304 2312 2460 2645 3038 3265 3760 3904 4421 2 NZ 705 817 907 917 954 1,026 1,065 1,125 1,276 1,449 > nz <-as.vector(data[2,2:11]) > nz V2 V3 V4 V5 V6V7V8V9 V10 V11 2 705 817 907 917 954 1,026 1,065 1,125 1,276 1,449 > class(nz) [1] "data.frame" [[alternative HTML version deleted]] __ R-help@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.
Re: [R] creating vectors with three variables out of three datasets
Is this what you want: > x V1 V2 V3 V4 1 ascii1: 11 12 13 2 ascii2: 14 15 16 3 ascii3: 17 18 19 > z <- as.matrix(x[,-1]) > z V2 V3 V4 [1,] 11 12 13 [2,] 14 15 16 [3,] 17 18 19 > as.vector(z) [1] 11 14 17 12 15 18 13 16 19 > On Thu, Nov 4, 2010 at 6:05 PM, DomDom wrote: > > okay sorry. > i´ve got three ascii files with pixel values without any header information. > > so if the first line of the three ascii files are: > > ascii1: 11 12 13 > ascii2: 14 15 16 > ascii3: 17 18 19 > > i would like a new matrix with: > 11,14,17;12,15,18;13,16,19; > > thx > > > -- > View this message in context: > http://r.789695.n4.nabble.com/creating-vectors-with-three-variables-out-of-three-datasets-tp3027852p3027880.html > Sent from the R help mailing list archive at Nabble.com. > > __ > R-help@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. > -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? __ R-help@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.
Re: [R] creating vectors with three variables out of three datasets
okay sorry. i´ve got three ascii files with pixel values without any header information. so if the first line of the three ascii files are: ascii1: 11 12 13 ascii2: 14 15 16 ascii3: 17 18 19 i would like a new matrix with: 11,14,17;12,15,18;13,16,19; thx -- View this message in context: http://r.789695.n4.nabble.com/creating-vectors-with-three-variables-out-of-three-datasets-tp3027852p3027880.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] creating vectors with three variables out of three datasets
DomDom wrote: Hi there, i´ve got a problem with how to create a vector with three variables out of three seperate ascii files. These three ascii files contain pixel information of the same image but different bands and i need a matrix of vectors, with each vector containing the corresponding pixel values for each band. Up to now i´ve seperately read out the ascii files into three matrices but don´t know how to put the corresponding pixel values together. Perhaps rbind or cbind, see ?rbind. It would be useful if you gave us a small, reproducible example of the type of data you have and what you want to do with it, please see the Posting Guide. Looking forward to any help. Thank you Dominik __ R-help@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. __ R-help@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.
[R] creating vectors with three variables out of three datasets
Hi there, i´ve got a problem with how to create a vector with three variables out of three seperate ascii files. These three ascii files contain pixel information of the same image but different bands and i need a matrix of vectors, with each vector containing the corresponding pixel values for each band. Up to now i´ve seperately read out the ascii files into three matrices but don´t know how to put the corresponding pixel values together. Looking forward to any help. Thank you Dominik -- View this message in context: http://r.789695.n4.nabble.com/creating-vectors-with-three-variables-out-of-three-datasets-tp3027852p3027852.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Creating vectors
You can use following scriptI think #create a vector of random numbers on which to test script v<-sample(1:3,size=90,replace=TRUE) #creates two matrixes out of vector v which can be assigned to M to test script M2<-matrix(v,ncol=2) M3<-matrix(v,ncol=3) M<- #Assign you're matrix or a testmatrix to M and run script result<-numeric() imaks<-length(M[,1]) jmaks<-length(unique(M)[,1]) for (i in 1:imaks){ for (j in 1:jmaks){ if (sum(M[i,]==unique(M)[j,])==length(M[1,])) { result[i]<-j } } } result ## The script uses loops so its not efficient - in other words its slow and might be too slow for larger matrixes. BR Jesper -- View this message in context: http://r.789695.n4.nabble.com/Creating-vectors-tp2321440p2323090.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Creating vectors
I think your code will work but only for the two columns I gave. I used those as an example but my actual data is 200 in length with two columns and I need code that will give a label to each unique pair but still have the original length for instance, one that will turn something such as [,1] [,2] [1,] 12 [2,] 23 [3,] 12 [4,] 46 [5,] 23 1 into one vector of length 5, giving a number for each unique pair (1,2,1,3,4) so that the same pairs get the same number. but my actual data is of length 200 with two columns Thanks. -- View this message in context: http://r.789695.n4.nabble.com/Creating-vectors-tp2321440p2322933.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Creating vectors
I think I understand your question and the following would produce the result you've posted. (x <- matrix(c(1, 2, 2, 3, 1, 2, 1, 2, 3, 4), nrow=5, byrow=TRUE)) On Aug 12, 2010, at 5:41 AM, clips10 wrote: Thanks for the help, I tried to apply this to a vector with two columns, well I suppose it is not a vector but for instance like this: [,1] [,2] [1,]1 2 [2,]2 3 [3,]1 2 [4,]1 2 [5,]3 4 and return a vector : 1,2,1,1,3, so that it recognises both columns together. I tried match(x, unique(x)) as earlier suggested but this returns a vector of length 10 as opposed to 5, even though unique(x) does remove the repeated rows. Sorry if this is confusing, I am trying to do as originally posted but with 2 columns Thanks -- View this message in context: http://r.789695.n4.nabble.com/Creating-vectors-tp2321440p2322646.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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. __ R-help@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.
Re: [R] Creating vectors
Thanks for the help, I tried to apply this to a vector with two columns, well I suppose it is not a vector but for instance like this: [,1] [,2] [1,]1 2 [2,]2 3 [3,]1 2 [4,]1 2 [5,]3 4 and return a vector : 1,2,1,1,3, so that it recognises both columns together. I tried match(x, unique(x)) as earlier suggested but this returns a vector of length 10 as opposed to 5, even though unique(x) does remove the repeated rows. Sorry if this is confusing, I am trying to do as originally posted but with 2 columns Thanks -- View this message in context: http://r.789695.n4.nabble.com/Creating-vectors-tp2321440p2322646.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Creating vectors
Here's another way... x <- c(2,2,4,6,2,4,4,6,8,6) match(x, unique(x)) Produces... [1] 1 1 2 3 1 2 2 3 4 3 On 12 August 2010 01:48, clips10 wrote: > > I didn't really know what to post as the topic subject, but I have a vector, > for instance (2,2,4,6,2,4,4,6,8,6) and I want to create another vector which > is just numbers from 1 to 4 since there are only 4 unique numbers in my > vector, so for instance 2 would be 1, 4 would be 2, 6 would be 3, and 8 > would be 4, so my new vector would be __ R-help@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.
Re: [R] Creating vectors
clips10 lancaster.ac.uk> writes: > I didn't really know what to post as the topic subject, but I have a vector, > for instance (2,2,4,6,2,4,4,6,8,6) [... snip to make gmane happy ...], > so my new vector would be (1,1,2,3,1,2,2,3,4,3). x <- c(2,2,4,6,2,4,4,6,8,6) as.numeric(factor(x)) [1] 1 1 2 3 1 2 2 3 4 3 __ R-help@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.
[R] Creating vectors
I didn't really know what to post as the topic subject, but I have a vector, for instance (2,2,4,6,2,4,4,6,8,6) and I want to create another vector which is just numbers from 1 to 4 since there are only 4 unique numbers in my vector, so for instance 2 would be 1, 4 would be 2, 6 would be 3, and 8 would be 4, so my new vector would be (1,1,2,3,1,2,2,3,4,3). The vector I have has longer numbers, in the thousands, but it's the same principle I want. I have a vector of length 200 but I know there are only 160 unique numbers in there, so would like a new vector that just has numbers between 1 and 160 correpsonding to each unique element of the original vector, and is the same length as the original. Sorry if this is not very clear, Thanks very much for any help in advance. -- View this message in context: http://r.789695.n4.nabble.com/Creating-vectors-tp2321440p2321440.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Creating vectors
Hi, Try ?unique please. x <- c(2,2,9,4,6,2,4,4,6,8,6) # Original vector unique(x) #New vector only has unique elements sort(unique(x)) # Ordered Regards, Wu - A R learner. -- View this message in context: http://r.789695.n4.nabble.com/Creating-vectors-tp2321440p2321884.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] creating vectors from a list
Try this: lapply(names(L), function(l)assign(sprintf('vector_%s', l), L[l], envir = globalenv())) ls() On Mon, Sep 28, 2009 at 11:57 AM, Christina Rodemeyer wrote: > Hi guys, > > I have a list of 250 numbers as a result of using the ?by function! > List of 246 > $ 0 : num [1:28] 22 11 31... > $ 1 : num [1:15] 12 14 9 ... > .. > .. > .. > - attr(*, "dim")= int 250 > - attr(*, "dimnames")=List of 1 > > The problem is that each list of 250 has different length! I would like to > get the values of each list in a vector like vector_0 = (22,11,31,..), is > this possible? > > Thank you in advance, > Christina > > > > > [[alternative HTML version deleted]] > > > __ > R-help@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. > > -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O __ R-help@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.
Re: [R] creating vectors from a list
Try this: > L <- list(`0` = 1:4, `1` = 2:3) > sum(L$`0`) [1] 10 > with(L, sum(`0`)) [1] 10 > # not recommended tho' this is closest to what you asked for > attach(L) > sum(`0`) [1] 10 On Mon, Sep 28, 2009 at 10:57 AM, Christina Rodemeyer wrote: > Hi guys, > > I have a list of 250 numbers as a result of using the ?by function! > List of 246 > $ 0 : num [1:28] 22 11 31... > $ 1 : num [1:15] 12 14 9 ... > .. > .. > .. > - attr(*, "dim")= int 250 > - attr(*, "dimnames")=List of 1 > > The problem is that each list of 250 has different length! I would like to > get the values of each list in a vector like vector_0 = (22,11,31,..), is > this possible? > > Thank you in advance, > Christina > > > > > [[alternative HTML version deleted]] > > > __ > R-help@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. > > __ R-help@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.
[R] creating vectors from a list
Hi guys, I have a list of 250 numbers as a result of using the ?by function! List of 246 $ 0 : num [1:28] 22 11 31... $ 1 : num [1:15] 12 14 9 ... .. .. .. - attr(*, "dim")= int 250 - attr(*, "dimnames")=List of 1 The problem is that each list of 250 has different length! I would like to get the values of each list in a vector like vector_0 = (22,11,31,..), is this possible? Thank you in advance, Christina [[alternative HTML version deleted]] __ R-help@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.