Reputation: 269
I think I'm just too tired to see the mistake. I wrote a function to get the maximal value for two data sets from a 'for' loop:
plot_zu <- function(x) {for (i in 1:x){
z=data_raw[grep(a[i], data_raw$Gene.names),]
b=data_raw_ace[grep(a[i], data_raw_ace$Gene.names),]
p<-vector("numeric", length(1:length(a)))
p[i]<-max(z$t_test_diff)
return(p)}
}
Imagine: a is a vector of names and the data set (data_raw(_ace))
is filtered by it. In the end, I would like to have all maxima values of column t_test_diff
in a vector. After that I want to add the t_test_diff
column values from data_raw_ace also.
So the problem is, that I get this:
[1] 1.210213 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
[8] 0.000000 0.000000
So there is a problem with brackets or something but I cannot see it ( first value fits). Sorry for no good example but i think it is understandable and an easy to solve question.
If need be, I can add another example.
Thanks a lot!!
gratefully,
Hendrik
Upvotes: 1
Views: 4651
Reputation: 263481
In the absence of data and even the call you make to this function, I'm going to offer an alternative based on what I think you are attempting. It appears you want to select only those rows of "data_raw" whose "Gene.names" column values are in the set defined by "a". If so, that is just:
z <- data_raw[ data_raw$Gene.names %in% a , ] # no loop needed
b <- data_raw_ace[ data_raw_ace$Gene.names %in% a , ] # again no loop needed
# Next step is unclear
If you want to use grep or grepl inside "[" then use sapply:
z <- data_raw[ sapply(a, grep, x= data_raw$Gene.names), ] # (still) no loop needed
b <- data_raw_ace[ sapply(a, grep, x= data_raw_ace$Gene.names), ]
When you do this, what is it that is desired?
p<-vector("numeric", length(1:length(a)))
p[i]<-max(z$t_test_diff)
If you want the maximum value for an identically named column in the two subset of data, then do this:
p <- pmax( z$t_test_diff, b$t_test_diff )
Based on you further comments above I (now) think maybe:
p <- apply( cbind(z$t_test_diff), abs(b$t_test_diff), 1, function(x) x[which.max(abs(x))])
Upvotes: 1
Reputation: 91
It appears you are overwriting p in each new iteration by defining it inside the loop.
Upvotes: 0
Reputation: 42689
vapply
does this, eliminating the for
loop. Not tested.
vapply(1:x, FUN.VALUE=numeric(1), FUN=function(x) {
z=data_raw[grep(a[i], data_raw$Gene.names),]
b=data_raw_ace[grep(a[i], data_raw_ace$Gene.names),] # Is this needed?
return(max(z$t_test_diff))
})
Upvotes: 0