merge adjacent integers into ranges? - r

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

Related

How to maximum values of multiple vectors element wise

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))

Is there a possibility to make from one long column two small in R?

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

How can I merge these two lists [duplicate]

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))

Performing sine function and arithmetical operations on a data frame with special columns(columns with lists)

I have a data frame df1 with list columns
df1 <- data.frame(w= 1:3, x=3:5, y=6:8, z = I(list(1:2, 1:3, 1:4)))
> df1
w x y z
1 1 3 6 1, 2
2 2 4 7 1, 2, 3
3 3 5 8 1, 2, 3, 4
I have transformed df1 into a second data frame df2 by multiplying every other column of df1 with the column z:
df2<- as.data.frame(do.call(cbind, lapply(df[1:3], function(x) Map("*", df$z, x))))
> df2
w x y
1 1, 2 3, 6 6, 12
2 2, 4, 6 4, 8, 12 7, 14, 21
3 3, 6, 9, 12 5, 10, 15, 20 8, 16, 24, 32
I want to create a third data frame , df3 by multiplying df2 by pi, then taking the sine function of the result and multiplying the sine output by 4 while preserving the data frame structure. I wish to be able to perform the operation with the most time-effective method possible since I am working with a much larger data frame:
df3 <- as.data.frame (4*sin(df2*pi))
Considering that the Map function accepts not more than two parameters, I have attempted a solution like this which unfortunately is taking a an infinitely long time:
df3 <- lapply(df2, function(x) Map("*",vol, Map("sin", Map("*",pi, x))))
I will be very grateful for any hint on how to perform the following operation in the most time-effective way.
df3 <- as.data.frame (4*sin(df2*pi))
My expected out put is to have a data frame df3 where each element
is the same as 4*sin(df2[i,k]).
Thank you in advance.
You don't need Map here. Map is a wrapper around mapply that autodefines SIMPLIFY = FALSE. For the creation of df3, you do not have multiple arguments, therefore lapply is sufficient:
df3 <- as.data.frame(do.call(cbind, lapply(df2, function(x){
lapply(x, function(y){
4*sin(y*pi)
})
})))
> df3
w x
1 4.898425e-16, -9.796851e-16 1.469528e-15, -2.939055e-15
2 -9.796851e-16, -1.959370e-15, -2.939055e-15 -1.95937e-15, -3.91874e-15, -5.87811e-15
3 1.469528e-15, -2.939055e-15, 4.408583e-15, -5.878110e-15 2.449213e-15, -4.898425e-15, 2.155849e-14, -9.796851e-15
y
1 -2.939055e-15, -5.878110e-15
2 3.428898e-15, -6.857796e-15, -3.924161e-15
3 -3.918740e-15, -7.837481e-15, -1.175622e-14, -1.567496e-14
You may flatten df2 to a vector and perform 4*sin(pi*x) in one call.
relist helps to transform back to the initial structure:
df3 <- 4 * sin(unlist(df2) * pi)
df3 <- as.data.frame(
do.call(cbind, relist(df3, as.list(df2))))

Unique elements of two vectors

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))

Resources