similar to: Odd subsetting behaviour

Displaying 20 results from an estimated 20000 matches similar to: "Odd subsetting behaviour"

2012 May 15
1
Odd behaviour of identical()
Consider the following code: test <- function(n) { for(x in 1:n) { for(y in 1:n) { for(r in max(x-1,1):min(x+1,n)) { for(s in max(y-1,1):min(y+1,n)) { vec <- c(x-r,y-s) print(c("vec = ", vec)) print(identical(vec,c(0,0))) } } } } } If you run test(2) you'll see a printout of the values
2005 Aug 29
2
reexpand a matrix after subsetting
Hi, suppose I have a matrix (or dataframe) as a result from subsetting. mat <- matrix(1:20,ncol=2) mat[c(3,6,9),] <- NA cc <- complete.cases(mat) sub <- mat[cc,,drop=FALSE] sub <- sub * 2 #some caluculations with sub. now I would like to expand sub somehow so row 3,6, and 9 would be filled with NAs but the rest should be in place again. Is there a simple function for this?
2010 Apr 08
2
subsetting a matrix with specified no of columns
Hello! All, I am working on 10000x1000 matrix say 'mat' and i want to subset this matrix in a fashion that in new matrix i get columns 2,3,9,10,16,17,23,24.......so on. That is pair of columns after every interval of 7. I tried following but i got an error which is obvious. >dim(mat) [1] 10000 10 >a=mat[,c(seq(c(2,3),ncol(mat),7))] Warning messages: 1: In if (n < 0L)
2009 Jun 24
3
List subsetting
Hello, I have a question about list indexing. Lets say we have a list of 3 lists, each containing 3 different type elements: > a=replicate(3, list(list(c(1,1,1), diag(3), c(2,2,2)))) > a [[1]] [[1]][[1]] [1] 1 1 1 [[1]][[2]] [,1] [,2] [,3] [1,] 1 0 0 [2,] 0 1 0 [3,] 0 0 1 [[1]][[3]] [1] 2 2 2 [[2]] [[2]][[1]] [1] 1 1 1 [[2]][[2]] [,1] [,2] [,3]
2005 Jan 14
5
Replacing NAs in a data frame using is.na() fails if there are no NAs
Hi This is a difference between the way matrices and data frames work I guess. I want to replace the NA values in a data frame by 0, and the code works as long as the data frame in question actually includes an NA value. If it doesn't, there is an error: df <- data.frame(c1=c(1,1,1),c2=c(2,2,NA)) df[is.na(df)] <- 0 df df <- data.frame(c1=c(1,1,1),c2=c(2,2,2)) df[is.na(df)] <-
2005 Sep 28
3
is it possible to form matrix of matrices...and multiple arrays
Dear sirs, 1...........Kindly tell me is it possible to form a matrix which contains a no of matrices.. for eg.. if a,b,c,d are matrices.... and e is a matrix which contains a,b,c,d as rows and columns.. 2..........Is it possible to form array of array of arrays for eg.. "A" contains two set of arrays (1,2)...and each A[1] and A[2] individually contains two set of arrays I tried like
2006 Dec 05
5
if(){} else{}
Dear R-community, my data set looks like 'mat' below. Plant<-c(NA,1,1,1,NA,NA,NA,NA,NA,1); Value1<-rnorm(1:10); Value2<-rnorm(1:10); mat<-cbind(Plant,Value1,Value2); I receive data from two different sites. One site is identified by an interger number, the other site has no data in column Plant=NA. My pb: I'm trying to assign labels "A" or "B" to
2013 Feb 21
3
Ask for help: find corresponding elements between matrix
Dear R experts, I have two matrix (seq & mat) & I want to retrieve in a new matrix all the numbers from mat that =1 (corresponding to the same row/ column position) in seq, or all the numbers in mat that =-1 in seq. - Replace all the numbers with NA if it's not 1/-1 in seq. There are some "NA"s in seq. seq=matrix(c(1,-1,0,1,1,-1,0,0,-1,1,1,NA),3,4)
2025 Mar 29
2
Setting up hypothesis tests with the infer library?
Hi, Rui and Michael, thank you both for replying. Yeah, I'm not supposed to know about Chi-squared yet. So far, all of our work with hypothesis tests has involved creating the sample data, then resampling it to create a null distribution, and finally computing p-values. The prop.test() would work. obviously. I'll look into that. I didn't know about it. I'm really struck by the
2013 Mar 08
2
Unexpected behaviour of apply()
Hello everyone, Considering the following code sample : ---- indexes <- function(vec) { vec <- which(vec==TRUE) return(vec) } mat <- matrix(FALSE, nrow=10, ncol=10) mat[1,3] <- mat[3,1] <- TRUE ---- Issuing apply(mat, 1, indexes) returns a 10-cell list, as expected. Now if I do: ---- mat[1,3] <- mat[3,1] <- FALSE apply(mat, 1, indexes) ---- I would expect a
2010 Mar 26
2
Odd results with %% and conserving memory
Can anyone explain this? I have a matrix with double components. It's taking up a lot of memory, so I want to multiply then turn it to integers. I'm pretty certain that there are only 2 decimal places, but I wanted to check by using modulo. E.g. mat = matrix(11:50/100, ncol=4,nrow=10) #Matrix with values out to the hundredths any((mat * 100)%%1!=0) But oddly enough it doesn't work.
2012 Mar 22
2
Bubble chart
I have a matrix of p-values for for each explanatory variable. Each row is an area of the response variable and each column is an explanatory variable. e.g. PSA pval_DOY pval_PDSIconcurrent pval_PDSIantecedent_annual_average pval_TMAXanomaly pval_FM100anomaly 1 NC06 0.96747495 0.6092668 0.53353019 0.9301115 0.99801334 2 NC04 0.04699659
2008 Oct 22
2
suboptimal lp solutions
Hi list, I want to find the total maximum resources I can spend given a set allocation proportion and some simple budget constraints. However, I get suboptimal results via lp and friends (i.e. lpSolve and simplex in the linprog and boot) . For example: library(lpSolve) proportions = c( 0.46, 0.28, 0.26) constraints = c( 352, 75, 171) lp(objective.in = proportions, const.mat =
2004 Dec 10
1
subset bug?
I ran into a problem with "subset" while working at home that I am not sure is a bug or not. I defined a custom function to take a data frame of tree positions, sizes, and types (containing the columns TRT, COMP, PLOT, X, Y, DBH and CON) and wanted to make a stem map of the plot using different symbols and colors to represent the different types of tree stems. I copied a bit of the
2010 Jan 26
3
Problem with "nls" function
Dear R users, I have a response variable in a csv file called "y" and a matrix of predictor variables in a csv file called "mat". I have used the function "nls" I have specified the nonlinear relation between these variable.The code I have witten is called Rprog which begins with the phrase: L.minor.m1<-nls(Y~a ....etc.. The program when I execute the program, I
2006 Feb 07
0
Subsetting matrices
Dear R-Helpers, I am working on a dataset containing ca. 500 specimens of 17 different species. The data is three text columns, followed by ca 70 columns of binary data. I'm trying to write a function that will allow me to interactively exclude species using the inofrmation in the third text column. My function, matrix.slicer, appears to work as I intend. However, when I pass the
2012 Jul 26
1
loop for, error: obj type 'closure' not subsetable
Hi everyone, I've got the following problem: I've got a matrix [1000,2] and two vectors. In very matrix row there is two coefficients b0 and b1. The vectors are two variables x and y. I want to do a loop to take b0 and b1 and with x and y calculate the residual of a linear model and calculate the second order coefficient of autocorrelation. What I did is : rho<-function(mat, x,y){
2019 Jul 13
2
head.matrix can return 1000s of columns -- limit to n or add new argument?
Hi Michael and Abby, So one thing that could happen that would be backwards compatible (with the exception of something that was an error no longer being an error) is head and tail could take vectors of length (dim(x)) rather than integers of length for n, with the default being n=6 being equivalent to n = c(6, dim(x)[2], <...>, dim(x)[k]), at least for the deprecation cycle, if not
2010 Feb 04
3
strange behaviour of median
During some experimentation in preparing R lessons I encountered this behaviour which I can not explain fully mat <- matrix(1:16, 4,4) df1 <- data.frame(mat) > mean(df1) X1 X2 X3 X4 2.5 6.5 10.5 14.5 Expected, documented > median(df1) [1] 6.5 10.5 Rather weird, AFAIK there shall not be an issue with data frame at least I did not find any in help page. I tracked it
2005 Sep 09
3
how to do something like " subset(mat, ("col1">4 & "col2">4)) "
Dear all, I have a problem with the "subset()" function. I spent all day yesterday with a collegue to solve it and we did not find a satisfying solution (even in the archived mails), so I ask for your help. Let's say (for a simple example) a matrix mat: R> mat cola colb colc [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9 My goal is to select the lines of the matrix on the basis of the