Displaying 20 results from an estimated 600 matches similar to: "histogram"
2013 Feb 27
2
matrix multiplication
Hi,
Try this:
#mat1 is the data
res<-do.call(cbind,lapply(seq_len(nrow(mat1)),function(i) {new1<-do.call(rbind,lapply(seq_len(nrow(mat1[-i,])),function(j) {x1<-rbind(mat1[i,],mat1[j,]); x2<-(abs(x1[1,1]-x1[2,1])*abs(x1[1,5]-x1[2,5]))+(abs(x1[1,2]-x1[2,2])*abs(x1[1,6]-x1[2,6]))+(abs(x1[1,3]-x1[2,3])*abs(x1[1,7]-x1[2,7]))+(abs(x1[1,4]-x1[2,4])*abs(x1[1,8]-x1[2,8]))}));new1}))
2013 Feb 17
1
addition in the initial question
Dear Elisa,
Try this:
vec1<-c(33,18,13,47,30,10,6,21,39,25,40,29,14,16,44,1,41,4,15,20,46,32,38,5,31,12,48,27,36,24,34,2,35,11,42,9,8,7,26,22,43,17,19,28,23,3,49,37,50,45)
vec2<-vec1[1:26]
names(vec2)<-LETTERS[1:26]
label1<-unlist(lapply(mapply(c,lapply(seq(0,45,5),function(x) x),lapply(seq(5,50,5),function(x) x),SIMPLIFY=FALSE),function(i)
2012 Dec 25
5
aggregate / collapse big data frame efficiently
Hi,
I need to aggregate rows of a data.frame by computing the mean for rows with the same factor-level on one factor-variable;
here is the sample code:
x <- data.frame(rep(letters,2), rnorm(52), rnorm(52), rnorm(52))
aggregate(x, list(x[,1]), mean)
Now my problem is, that the actual data-set is much bigger (120 rows and approximately 100.000 columns) ? and it takes very very long
2013 Sep 02
1
Convert chr pieces to numbers that have specific values defined by 2 vectors
Dear all,
I think this is an easy task, but I don't know how to do it. Specifically, I have 69 columns with 300.000 rows. In each cell there is a code like
"2E3", "4RR", etc.
I now have a list that replaces this with values, e.g.,
old new
2E3 5
4RR 3
etc.
The list ist about 1600 rows long, so also to extensive for normal solutions
Do you how to do that? It would
2013 Oct 14
1
R Help-how to use sapply w/tapply
Hi,
(Please use ?dput() to share the example dataset. Avoid using images to show dataset. Also, please read the posting guide esp. regarding home work, assignments etc.)
res <- sapply(Gene[,-1],function(x) tapply(x,list(Gene$Genotype),mean))
#or
res2 <-? aggregate(.~Genotype, data=Gene,mean)
#or
library(plyr)
?res3 <- ddply(Gene,.(Genotype),numcolwise(mean))
identical(res2,res3)
2013 Sep 02
1
R dataframe and looping help
HI,
You may try this:
dat1<- read.table(text="
CustID TripDate Store Bread Butter Milk Eggs
1 2-Jan-12 a 2 0 2 1
1 6-Jan-12 c 0 3 3 0
1 9-Jan-12 a 3 3 0 0
1 31-Mar-13 a 3 0 0 0
2 31-Aug-12 a 0 3 3 0
2 24-Sep-12 a 3 3 0 0
2 25-Sep-12 b 3 0 0 0
",sep="",header=TRUE,stringsAsFactors=FALSE)
dat2<- dat1[,-c(1:3)]
res<- lapply(seq_len(ncol(dat2)),function(i)
2013 Jun 08
1
splitting a string column into multiple columns faster
Hello!
I have a column in my data frame that I have to split: I have to distill
the numbers from the text. Below is my example and my solution.
x<-data.frame(x=c("aaa1_bbb1_ccc3","aaa2_bbb3_ccc2","aaa3_bbb2_ccc1"))
x
library(stringr)
out<-as.data.frame(str_split_fixed(x$x,"aaa",2))
out2<-as.data.frame(str_split_fixed(out$V2,"_bbb",2))
2012 Aug 24
6
updating elements of a vector sequentially - is there a faster way?
I would like to know whether there is a faster way to do the below
operation (updating vec1).
My objective is to update the elements of a vector (vec1), where a
particular element i is dependent on the previous one. I need to do this on
vectors that are 1 million or longer and need to repeat that process
several hundred times. The for loop works but is slow. If there is a faster
way, please let
2002 Jun 18
5
insert number in vector
Hello R-users,
I need to create a vector inserting an 1 after each value of another vector.
For example:
vec1<-c(2,3,4)
I need to create a vector with the values 2,1,3,1,4
Does anyone know how create this vector without loops (vec1 could have 1000
elements)
Thank you,
Juan
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read
2024 Jun 16
1
slowness when I use a list comprehension
This can be vectorized. Try
ix <- seq_along(vec2)
S_diff2 <- sapply(seq_len(N1-(N2-1)*ratio_sampling), \(j)
sum((vec1[(ix-1)*ratio_sampling+j] - vec2[ix])**2))
On Sun, Jun 16, 2024 at 11:27?AM Laurent Rhelp <laurentRHelp at free.fr> wrote:
>
> Dear RHelp-list,
>
> I try to use the package comprehenr to replace a for loop by a list
> comprehension.
>
> I
2010 Feb 02
1
Build a matrix from another matrix by specifying the indexes
Hello R specialists,
I have a base vector called vBase with 102 elements. I have another vector
vec1 which elements are only part of vBase but is shorter. I transform vec1
so I get a vector with the same size as vBase and with each common element
on the same indexed row. If a value is missing in vec1 then I put a Na like
this:
vec1 vBase
Amsterdam Amsterdam
Na
2012 Nov 08
5
map two names into one
Thanks.
Yes. Your approach can identify:
Glaxy ace S 5830 and
S 5830 Glaxy ace
But you can not identify using same program:
Iphone 4S 16 G
Iphone 4S 16G
How should I solve both in same time.
Kind regards,Tammy
[[alternative HTML version deleted]]
2008 May 12
1
lexicographic comparison of two vectors
Is there any built-in way to lexicographically compare two vectors of
the same length in R? The textbook algorithm could be coded as follows:
lex.cmp <- function (vec1,vec2) {
for (j in 1:length(vec1)) {
if (vec1[j] < vec2[j]) { return(-1) }
if (vec1[j] > vec2[j]) { return(1) }
}
return(0)
}
Thanks,
Gabriel
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]
2010 Aug 24
3
multiple assignments ?
Simple one, have read and googled, still no luck!
I want to create several empty vectors all of the same length.
I would like multiple empty vectors (vec1, vec2, vec3) and want to create them all in one line.
I've tried
vec1,vec2,vec3 <- vector(length=5)
and
c(vec1,vec2,vec3) <- vector(length=5)
and several other attempts but nothing seems to work ... suggestions?
Thanks
Jim
2006 Feb 13
2
?bug? strange factors produced by chron
Hallo all
Please help me. I am lost and do not know what is the problem. I have
a factor called kvartaly.
> attributes(kvartaly)
$levels
[1] "1Q.04" "2Q.04" "3Q.04" "4Q.04" "1Q.05" "2Q.05" "3Q.05" "4Q.05"
$class
[1] "factor"
> mode(kvartaly)
[1] "numeric"
> str(kvartaly)
Factor w/ 8
2013 Mar 22
2
A question on function return
Hello again,
Let say I have following user defined function:
fn <- function(x, y) {
Vec1 <- letters[1:6]
Vec2 <- 1:5
return(list(ifelse(x > 0, Vec1, NA), ifelse(y > 0, Vec2, NA)))
}
Now I have following calculation:
> fn(-3, -3)
[[1]]
[1] NA
[[2]]
[1] NA
> fn(3, -3)
[[1]]
[1] "a"
[[2]]
[1] NA
Here I can not understand why in the second case, I get
2013 Jun 18
1
transform 3 numeric vectors empty of 0/1
Dear all,
Without a loop, I would like transform 3 numeric vectors empty of 0/1 of
same length
Vec1 : transform 1 to A and 0 to ""
Vec2 : transform 1 to B and 0 to ""
Vec3 : transform 1 to C and 0 to ""
to obtain only 1 vector Vec who is the paste of the 3 vectors (Ex : ABC,
BC, AC, AB,...)
Any idea ?
Thank you for your help
--
Michel ARNAUD
2024 Jun 16
2
slowness when I use a list comprehension
Dear RHelp-list,
?? I try to use the package comprehenr to replace a for loop by a list
comprehension.
?I wrote the code but I certainly miss something because it is very
slower compared to the for loops. May you please explain to me why the
list comprehension is slower in my case.
Here is my example. I do the calculation of the square difference
between the values of two vectors vec1 and
2009 Jul 27
1
how to use do.call together with cbind and get inside a function
Dear R-helpers:
I have a question related to using do.call to call cbind and get.
#the following works
vec1 <- c(1,2)
vec2 <- c(3,4)
ColNameVec <- c('vec1','vec2')
mat <- do.call("cbind",lapply(ColNameVec,get))
mat
#put code above into a function then it does not work
#before doing so, first remove vec1 and vec2 from global environment
rm(vec1,vec2)
test