[Solved] function for matrix

I’ve tried this, looking at wikipedia. http://en.wikipedia.org/wiki/Invertible_matrix#Blockwise_inversion getInverse <- function(mat) { if(nrow(mat) == 1) { return (matrix( 1.0/ mat[1,1] )) } idx <- nrow(mat) / 2 A <- mat[1:idx, 1:idx, drop=F] B <- mat[1:idx, -1:-idx, drop=F] C <- mat[-1:-idx, 1:idx, drop=F] D <- mat[-1:-idx, -1:-idx, drop=F] invA <- getInverse(A) temp <- getInverse(D – C %*% … Read more

[Solved] Efficient dynamic addition of rows in dataframe and dynamic calculation in R

Here is the complete solution: The usage of the last command of linear interpolation solves the issue > Lines <- “D1,Value + 1,20/11/2014 16:00,0.00 + 2,20/11/2014 17:00,0.01 + 3,20/11/2014 19:00,0.05 + 4,20/11/2014 22:00,0.20 + 5,20/11/2014 23:00,0.03” > ts1 <- read.csv(text = Lines, as.is = TRUE) > library(zoo) > z <- read.zoo(ts1, tz = “”, format … Read more

[Solved] in R, How to sum by flowing row in a data frame

We could use shift from data.table library(data.table) m1 <- na.omit(do.call(cbind, shift(df1$col1, 0:4, type=”lead”))) rowSums(m1*(1:5)[col(m1)]/5) #[1] 13.60 12.20 31.24 25.58 30.48 32.58 44.88 Or another option m1 <- embed(df1$col1,5) rowSums(m1*(5:1)[col(m1)]/5) #[1] 13.60 12.20 31.24 25.58 30.48 32.58 44.88 solved in R, How to sum by flowing row in a data frame

[Solved] Subset columns in R with specific values

Assuming you mean subset columns that all of their values are greater than 1 you could do something like this (essentially you can also use the following according to any condition you might have. Just change the if condition): Example Data a <- data.frame(matrix(runif(90),ncol=3)) > a X1 X2 X3 1 0.33341130 0.09307143 0.51932506 2 0.78014395 … Read more

[Solved] Improving my R code – advice wanted on better way of coding? [closed]

You could try using purrr instead of the loop as follows: require(rvest) require(purrr) require(tibble) URLs %>% map(read_html) %>% map(html_nodes, “#yw1 .spielprofil_tooltip”) %>% map_df(~tibble(Player = html_text(.), P_URL = html_attr(., “href”))) Timing: user system elapsed 2.939 2.746 5.699 The step that take the most time is the crawling via map(read_html). To paralyze that you can use e.g. … Read more

[Solved] What does subset(df, !duplicated(x)) do?

The duplicated function traverses its argument(s) sequentially and returns TRUE if there has been a prior value identical to the current value. It is a generic function, so it has a default definition (for vectors) but also a definition for other classes, such as objects of the data.frame class. The subset function treats expressions passed … Read more

[Solved] Turn extraction list to csv file

If I understand what it is you’re asking, I think you could resolve your situation by using unlist(). d <- c(1:10) # creates a sample data frame to use d <- as.list(d) # converts the data frame into a list d <- unlist(d) # converts the list into a vector 0 solved Turn extraction list … Read more

[Solved] How do I break up a dataset in R so that particular values of a categorical variable are together, and I can then perform analysis of those values? [closed]

A useful package for this may be dplyr. I’ll use a couple of bogus variables from the included iris dataset to give an indication of how this would work. In your case, replace iris with your dataset, and change the various calculations to what you need. require(dplyr) iris %>% mutate(# Calculate required variables at the … Read more

[Solved] R fulfill empty cells with previous values [closed]

Assuming that the first column contain blanks (“”) and is character class, we convert the blanks to NA and use na.locf to replace the NA elements with the non-NA previous element, then paste the output with the second column and create a new column (“colN”). library(zoo) library(data.table) setDT(df1)[col1==””, col1:= NA][, colN := as.numeric(paste0(na.locf(col1), col2))] df1 … Read more

[Solved] Theme for ggplot2 in r

First define a function with the elements that need to be changed as arguments. Set those arguments to defaults in the graph you really like. To change the defaults, pass them on to the function in its calls. library(ggplot2) my_theme <- function(plot_title_size = 30, colour = “black”, size = 20, angle= 0){ theme(plot.title = element_text(family … Read more