Displaying 20 results from an estimated 10000 matches similar to: "Faster way to implement this search?"
2011 Apr 06
5
Need a more efficient way to implement this type of logic in R
I have cobbled together the following logic. It works but is very
slow. I'm sure that there must be a better r-specific way to implement
this kind of thing, but have been unable to find/understand one. Any
help would be appreciated.
hh.sub <- households[c("HOUSEID","HHFAMINC")]
for (indx in 1:length(hh.sub$HOUSEID)) {
if ((hh.sub$HHFAMINC[indx] == '01')
2013 Oct 11
3
matrix values linked to vector index
Hi,
In the example you showed:
m1<- matrix(0,length(vec),max(vec))
1*!upper.tri(m1)
#or
?m1[!upper.tri(m1)] <-? rep(rep(1,length(vec)),vec)
#But, in a case like below, perhaps:
vec1<- c(3,4,5)
?m2<- matrix(0,length(vec1),max(vec1))
?indx <- cbind(rep(seq_along(vec1),vec1),unlist(tapply(vec1,list(vec1),FUN=seq),use.names=FALSE))
m2[indx]<- 1
?m2
#???? [,1] [,2] [,3] [,4] [,5]
2013 Mar 22
3
Distance calculation
Hi Elisa,
I hope this is what you wanted.
dat1<-read.csv("peaks.csv",sep=",")
#Subset
dat2<-dat1[1:5,]
res1<-do.call(cbind,lapply(seq_len(nrow(dat2)),function(i) do.call(rbind,lapply(split(rbind(dat2[i,],dat2[-i,]),1:nrow(rbind(dat2[i,],dat2[-i,]))), function(x) {x1<-rbind(dat2[i,],x);
2013 Oct 04
3
Trying to avoid nested loop
Dear R users.
I'm trying to avoid using nested loops in the following code but I'm not sure how to proceed. Any help would be greatly appreciated.
With regards,Phil
X = matrix(rnorm(100), 10, 10)
## Version with nested loopsresult = 0
for(m in 1:nrow(X)){ for(n in 1:ncol(X)){ if(X[m,n] != 0){ result = result + (X[m,n] / (1 + abs(m - n))) } }}
## No loop-sum(ifelse(M
2013 Apr 24
2
Distance matrices Combinations
Dear UseRs,
MY PROBLEM IS A SMALL PIECE OF A REAL BIG AND A COMPLICATED PROBLEM. IF I DELIBERATE IN A VERY SIMPLE WAY THEN ALL I
WANT IS TO PUT ALL THE POSSIBLE COMBINATIONS OF 75 DISTANCE MATRICES (BY TAKING 4 MATRICES, MORE COMMONLY 75C4), in the following equation.
t<-as.matrix((MAT1)^2+(MAT2)^2+(MAT3)^2+(MAT4)^2+,upper=T,diag=T))
Then "1215450" values of "t"(one for
2006 May 26
2
combinatorial programming problem
Hola!
I am programming a class (S3) "symarray" for
storing the results of functions symmetric in its
k arguments. Intended use is for association indices
for more than two variables, for instance coresistivity
against antibiotics.
There is one programming problem I haven't solved, making an inverse
of the index function indx() --- se code below. It could for instance
return the
2010 Feb 22
1
shash in unique.c
Looking at shash in unique.c, from R-2.10.1 I'm wondering if it makes sense
to hash the pointer itself rather than the string it points to?
In other words could the SEXP pointer be cast to unsigned int and the usual
scatter be called on that as if it were integer?
shash would look like a slightly modified version of ihash like this :
static int shash(SEXP x, int indx, HashData *d)
{
2005 Sep 01
1
More block diagonal matrix construction code
Folks:
In answer to a query, Andy Liaw recently submitted some code to construct a
block diagonal matrix. For what seemed a fairly straightforward task, the
code seemed a little "overweight" to me (that's an American stock analyst's
term, btw), so I came up with a slightly cleaner version (with help from
Andy):
bdiag<-function(...){
mlist<-list(...)
## handle case in
2009 Sep 14
2
Escaping . in regular expression
If I run
cvec<-c("test.f", "test.sf", "try.g","try.res", "try.f")
print(cvec)
indx<-grep('\.f',cvec,perl=TRUE)
fset<-cvec[indx]
print(fset)
I get
> cvec<-c("test.f", "test.sf", "try.g","try.res", "try.f")
> print(cvec)
[1] "test.f" "test.sf"
2013 Apr 14
1
possible loop problem
Hi,
It would be better if you provided the output of dput(dataset).? I am not sure about the structure of your dataset.
Just from reading the data as is shown.
dat1<- read.table(text="
separator,tissID
>,>,2
,2,1
,6,5
,11,13
>,>,4
,4,9
,6,2
,7,3
,21,1
,23,58
,25,9
,26,4
>,>,11
,1,12
>,>,21
,4,1
,11,3
2017 Aug 09
2
Package nleqslv ERROR
Dear all,
I am relatively new to R and have had some difficulty in understanding an error i get when running a code to solve a system of non-linear equations, with four equations and two variables.
This is my code:
ALPHA <- c(-0.0985168033402, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4)
BETA <- c(-0.0985168033402, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4)
GAMMA <- c(0.3940672148378, 0.1, 0.15,
2008 Aug 16
1
unique.default problem (PR#12551)
Full_Name: Vilmos Prokaj
Version: R 2.7.1
OS: windows
Submission from: (NULL) (213.181.195.84)
Dear developers,
The following line of code (produced by a mistake) caused an infinite loop
unique("a",c("a","b"))
or also
unique(1,1:2)
I made a little investigation, and it seems to be that the following function
from unique.c is looping infinitely
static int
2017 Oct 12
4
comparing two strings from data
Hi,
I have two columns that contain numbers along with letters (as shown below)
and have different lengths. Each entry in the first column is likely to be
found in the second column at most once.
For each entry of the first column, if that entry is found in the second
column, I would like to get the corresponding index. For instance, if the
first entry of the first column is 5th entry in the
2013 Sep 26
1
Grouping Matrix by Columns; OHLC Data
HI,
May be this helps:
set.seed(24)
?mat1<- matrix(sample(1:60,30*24,replace=TRUE),ncol=24)
colnames(mat1)<- rep(c("O","H","L","C"),6)
indx<-seq_along(colnames(mat1))
n<- length(unique(colnames(mat1)))
?res<- lapply(split(indx,(indx-1)%%n+1),function(i) mat1[,i])
lapply(res,head,2)
#$`1`
#????? O? O? O? O? O? O
#[1,] 18 56 51 24 24 52
#[2,]
2017 Aug 09
0
Package nleqslv ERROR
Santi,
In the second line of your function you have the following:
f <- numeric(length(x))
This sets the length of this numeric vector (i.e., "f") to the length of the vector "x".
Later, inside the function you assign to values to 4 elements of the vector "f".
This assumes that "f" is at least 4 element in length.
However, you define
2010 Aug 13
1
loop for inserting rows in a matrix
Dear R friends,
I have a matrix with 2060 rows and 41 columns. One column is Date, another is Transect, and another is Segment. I want to ensure that there are 9 Transects (1 to 9) for each Date, and 8 Segments (1 to 8) for each Transect in the matrix, by inserting rows where these are missing.
I am new to coding, but am trying to write a loop which checks if each of the transects already
2012 Dec 11
1
Debian packaging and openblas related crash when profiling in R
Hello R-sig-debian and (hopefully) Dirk:
On Debian wheezy, I have the R packaging that CRAN (you) provide. I
run into a little trouble while trying to fiddle with alternative
BLAS.
I know you and I went around on this last year and I think perhaps
I've found something wrong in the framework, or I've just done
something wrong.
I installed the packages openblas-base and openblas-dev, and
2017 Oct 12
0
comparing two strings from data
It's generally a very good idea to examine the structure of data after you have read it in. str(data2) would have shown you that read.csv() turned your strings into factors, and that's why the == operator no longer does what you think it does.
use ...
data_2 <- read.csv("excel_data.csv", stringsAsFactors = FALSE)
... to turn this off. Also, the %in% operator will achieve
2017 Oct 13
1
comparing two strings from data
Combining and completing the advice from Greg and Boris the complete
solution is two lines:
data_2 <- read.csv("excel_data.csv", stringsAsFactors = FALSE)
match_list <- match( data_2$data1, data_2$data2 )
The vector match_list will have the matching position when it exists and
NA's otherwise. Its length will be the same as the length of data_2$data1.
You should get
2013 Jan 03
4
Index out SNP position
Dear R experts,
I have 2 matix: A& B. I am trying to index B against A - (1) find out B rows that fall between the col 1 and 2 of A& put them into a new vector SNP.I made code as below, but I cannot think of a right way to do it. Could anyone help me with the code? Thanks,Jiang----
A <-