[R] Matrix manipulation in for loop
Hi all, I have a problem with getting my code to do what I want! This is the code I have: create.means.one.size-function(nsample,var,nboot){ mat.x-matrix(0,nrow=nboot,ncol=nsample) for(i in 1:nboot){ mat.x[i,]-sample(var,nsample,replace=T) } mean.mat-rep(0,nboot) for(i in 1:nboot){ mean.mat[i]-mean(mat.x[i,]) } sd.mean-sd(mean.mat) return(mean.mat) } where nsample here is a scalar. Then this is where I have the problem create.all.means-function(nsample,var,nboot){ MEANS-matrix(0,nrow=nboot,ncol=length(nsample)) for(j in nsample){ MEANS[,]-create.means.one.size(j,var,nboot) } return(A=list(MEANS=MEANS,nsample=nsample,std.dev=sd(MEANS))) } here nsample is a vector of the different sample sizes I want. This function should first create an empty matrix MEANS then it loops through the possible values of nsample each time it calls create means.one.size() and puts the output into the appropriate column of means. this function outputs the matrix MEANS and the vector nsample - both as part of a list However, the vector nsample could be c(1,3,5) but I want the outputs to go into columns 1, 2 and 3 rather than 1,3,5 (which I'm not even sure would work anyway!) Any help would be great! If I leave the code as it is and use the vector data-c(1,2,3) Then create.all.means(c(1,2),data,5) gives $MEANS [,1] [,2] [1,] 2.5 2.5 [2,] 1.0 1.0 [3,] 2.0 2.0 [4,] 2.0 2.0 [5,] 2.0 2.0 $nsample [1] 1 2 $std.dev1] 0.5477226 0.5477226[ which obviously isn't correct :( -- View this message in context: http://r.789695.n4.nabble.com/Matrix-manipulation-in-for-loop-tp3525849p3525849.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
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
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.
[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.
[R] distance matrix?
I have a vector 0 to 10 and want to create a matrix with the differences between the numbers in it for instance: 0 1 2 3 4 5 6 7 8 9 10 0 0 1 2 3 4 5 6 7 8 9 10 1 1 0 1 2 3 4 5 6 789 2 3 4 5 6 7 8 9 10 Etc etc. So that the matrix is filled with the differences between in absolute value so there are no negatives. Any ideas? Thanks -- View this message in context: http://r.789695.n4.nabble.com/distance-matrix-tp2270722p2270722.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] Vector manipulation, for loop needed?
Hi, I have a vector of time in days t-1:48. I have observations and the day they were recorded. I also have a vector, S which takes different values depending on which day the observation was recorded. For example if on day 1 all in vector S get a value of 46/48, on day 2 get 42/48, day 3 38/48, decreasing by 4/48 each time until day 12 gets the value 2/48. Then for any day greater than 12 the vector S has value 0. I want to create a vector which gives the values of S in terms of days, t. I have tried: t-1:48 for(i in 1:t){ S-rep(0,48) if(t12){ S[i]-(48-3*t)/48} else{ S[i]-0} } but this doesn't work. I'm fairly new to R and don't have much skill in manipulating things. I'm trying to plot time against this vector so need it in terms of t. Sorry that this isn't even very clear in what I want and I'm aware that the (48-3*t)/48 won't even give me the 46/48,42/48,38/482/48,00 pattern I'm after but no idea what to do! Any help would be great. Thanks -- View this message in context: http://r.789695.n4.nabble.com/Vector-manipulation-for-loop-needed-tp2260334p2260334.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] Plotting different symbols in R for different values in a vector
I have some point locations I want to add to a plot and have the command pts1-as.points(Xcoordinate,Ycoordinate) then plot(poly1,type=l,lwd=1) what I want to do is add the locations onto the plot. I can do this using the points command but I have another vector in my data frame with the x and y coordinates called A for instance which takes values 1 and 0. I want to plot the locations onto my plot and have a different symbol or colour depending on whether this other vector takes the value 1 or 0. Is there anyway to do this? Thanks -- View this message in context: http://r.789695.n4.nabble.com/Plotting-different-symbols-in-R-for-different-values-in-a-vector-tp2258833p2258833.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.