Hi I would like to find a maximum values for each element from multiple vectors. For example:
v1<-c(1,1,3,5,10)
v2<-c(10,2,1,1,5)
v3<-c(11,4,2,1,9)
list_of_vectors <- list(v1, v2, v3)
I would like the result to be:
vmax<-c(11,4,3,5,10)
I know that there is the pmax function, so I tried it. Because I have the vectors as a list, I did it like this:
do.call(pmax, list_of_vectors, na.rm=TRUE)
But there is an error with the solution. How can I solve this?
Assuming the vectors are always the same length and are in a list as individual elements, then
matrixStats::colMaxs(do.call(rbind, l1))
#[1] 11 4 3 5 10
or your way,
do.call(pmax, c(l1, na.rm=TRUE))
#[1] 11 4 3 5 10
where
dput(l1)
list(c(1, 1, 3, 5, 10), c(10, 2, 1, 1, 5), c(11, 4, 2, 1, 9))
Related
I want to merge adjacent integers into ranges.
For example, suppose the input is the following.
R> c(1,2,3, 5,6, 9,10,11,12)
The output should be a matrix of the range boundary. The result for the example should be.
rbind(
c(1, 3)
, c(5, 6)
, c(9,12)
)
How to perform this operation in an efficient way? I suppose that vectorized functions should be called to speed things up, as for-loop, although can solve the problem, is not efficient.
Split at cumsum where differences are unequal to 1 and apply range using by for instance.
x <- c(1, 2, 3, 5, 6, 9, 10, 11, 12)
do.call(rbind, by(x, cumsum(c(1, diff(x)) != 1), range))
# [,1] [,2]
# 0 1 3
# 1 5 6
# 2 9 12
I will try to make my point. Imagine I've got a vector with an unknown length and/or content like the next one:
col.vector = c(1, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 5, 5)
I want to extract all the elements that are repeated "n" times. Lets say 3 times, then I want to be returned a vector with the respective elements: c(3, 4).
Is there any simple function?
We can use table if it is based on frequency
v1 <- table(col.vector)
as.integer(names(v1)[v1 == 3])
[1] 3 4
If it is based on repeating elements, use rleid
library(data.table)
v1 <- table(rleid(col.vector))
as.integer(names(v1)[v1 == 3])
One option could be:
with(rle(col.vector), values[lengths == 3])
[1] 3 4
Another way with table -
n <- 3
names(Filter(function(x) x == n, table(col.vector)))
#[1] "3" "4"
Hope you have a nice day.
Today I was trying two make from one big column two small ones in R. However, I haven't found a way how to make it.
I have something like this (however, it is way bigger)
name3 <- c(1, 2, 3, 4, 5, 6)
df1 <- data.frame(name3)
print(df1)
I want to do something like this. My intention is just take the total number of variables and divide it into two equal groups.
name <- c(1, 2, 3)
name1 <- c(4, 5, 6)
df <- data.frame(name, name1)
print (df)
Thanks in advance!
One way to do it, you can first write this as a matrix in which you specify the number of columns
than transform the matrix to dataframe
from a dataframe you can convert each column to a vector
This is how I did it
name3 <- c(1, 2, 3, 4, 5, 6)
df <- as.data.frame(matrix(name3, ncol = 2))
name1 <- df$V1
name2 <- df$V2
Trying to accomplish this as close to base r as possible, this would be my method if the order of the sub vector don't matter:
# needed for index function
library(zoo)
# simple function to calculate even / odd
is.even <- function(x) x %% 2 == 0
# define my vector of values
name3 <- c(1, 2, 3, 4, 5, 6)
# split vector by even or odd index.
split(name3,f= is.even(index(name3)) )
Result:
$`FALSE`
[1] 1 3 5
$`TRUE`
[1] 2 4 6
I have two vectors and I need to find out the unique elements in both, together.
I tried doing length(summary(merge(v1, v2))) but summary aggregates a bunch of my dataset because there is only one of those entries, so I get an incorrect length.
E.g.:
list_1 <- c(1,2,3,4,5,5,6,1,2,3)
list_2 <- c(2,3,4,5,10,11,10)
and the outcome should be
1,2,3,4,5,6,10,11
P.S. bonus points if you can return all the unique elements in a vector... :-)
It sounds like you're looking for union:
> union(v1, v2)
[1] 1 2 3 4 5 6 10 11
here is my solution.
p1 <- c(1, 4, 1, 1, 4, 5, 6, 7, 8)
p2 <- c(3, 4, 1, 6, 90, 10, 32)
unique(c(p1, p2))
You can use unlist with union
unlist(union(a,b))
I have two vectors and I need to find out the unique elements in both, together.
I tried doing length(summary(merge(v1, v2))) but summary aggregates a bunch of my dataset because there is only one of those entries, so I get an incorrect length.
E.g.:
list_1 <- c(1,2,3,4,5,5,6,1,2,3)
list_2 <- c(2,3,4,5,10,11,10)
and the outcome should be
1,2,3,4,5,6,10,11
P.S. bonus points if you can return all the unique elements in a vector... :-)
It sounds like you're looking for union:
> union(v1, v2)
[1] 1 2 3 4 5 6 10 11
here is my solution.
p1 <- c(1, 4, 1, 1, 4, 5, 6, 7, 8)
p2 <- c(3, 4, 1, 6, 90, 10, 32)
unique(c(p1, p2))
You can use unlist with union
unlist(union(a,b))