Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to apply two functions one function on the block diagonal and the second function on the off-diagonal elements in the data frame

df<- data.frame(a=c(1:10), b=c(21:30),c=c(1:10), d=c(14:23),e=c(11:20),f=c(-6:-15),g=c(11:20),h=c(-14:-23),i=c(4:13),j=c(1:10))

In this data frame, I have three block-diagonal matrices which are as shown in the image below enter image description here

I want to apply two functions, one is the sine function for block diagonal and the second is cosine function for the other elements and generates the same structure of the data frame.

sin(df[1:2,1:2])
sin(df[3:5,3:5])
sin(df[6:10,6:10])
cos(the rest of the elements)
like image 694
Jisika Avatar asked May 22 '20 12:05

Jisika


People also ask

What is the advantage of using a function over a Dataframe?

It allows you to just shoot over x number of columns and not deal with the dataframe in the function, so it's great for functions you don't control or doing something like sending 2 columns and a constant into a function (i.e. col_1, col_2, 'foo').

How to apply the apply() function to each column and row?

To apply the function to each column, pass 0 or 'index' to the axis parameter which is 0 by default. And to apply the function to each row, pass 1 or 'columns' to the axis parameter. The examples below illustrate the difference. Let’s look at some of the use-cases of the apply () function through examples. 1.

How to use Eisenhower_action function to apply to Dataframe rows?

The Eisenhower Action for a task (i.e. a row in the DataFrame) is computed by using the due_date and priority columns: The integer 2 means that the needed action is to SCHEDULE. In the rest of the article, we will evaluate 12 alternatives for applying eisenhower_action function to DataFrame rows.

How to call two functions under “onclick” in VS Code?

Above, we have set a function under “onclick” to call two other functions − In this way, work around the fun1 () and fun2 () as in the complete code below − To run the above program, save the file name “anyName.html (index.html)” and right click on the file. Select the option “Open with Live Server” in VS Code editor.


1 Answers

1) outer/arithmetic Create a logical block diagonal matrix indicating whether the current cell is on the block diagonal or not and then use that to take a convex combination of the sin and cos values giving a data.frame as follows:

v <- rep(1:3, c(2, 3, 5))
ind <- outer(v, v, `==`)
ind * sin(df) + (!ind) * cos(df)

2) ifelse Alternately, this gives a matrix result (or use as.matrix on the above). ind is from above.

m <- as.matrix(df)
ifelse(ind, sin(m), cos(m))

3) Matrix::bdiag Another approach is to use bdiag in the Matrix package (which comes with R -- no need to install it).

library(Matrix)

ones <- function(n) matrix(1, n, n)
ind <- bdiag(ones(2), ones(3), ones(5)) == 1

Now proceed as in the last line of (1) or as in (2).

like image 88
G. Grothendieck Avatar answered Sep 19 '22 15:09

G. Grothendieck