logo
down
shadow

How to perform row mean in matrix by 3 number of columns in sequence using R


How to perform row mean in matrix by 3 number of columns in sequence using R

By : We Got Served
Date : November 19 2020, 03:01 PM
Hope that helps I have a matrix with dimension 100x300 where I want to reduce number of columns, 300, into 100 columns by averaging per 3 columns, i.e. columns 1-3, become the new column 1, columns 4-6 becomes the new column 2, and so forth. So that my 100x300 matrix becomes a 100x100 matrix. , Here's an example of averaging 9 columns into 3 columns
code :
#DATA
set.seed(42)
m = matrix(rnorm(90), ncol = 9)

sapply(split(sequence(NCOL(m)), ceiling(sequence(NCOL(m))/3)), function(i) rowMeans(m[,i]))
#                1          2           3
# [1,]  0.78972984  0.3277913  0.03411781
# [2,] -0.01978707 -0.1466863  0.11765521
# [3,] -0.39921655  1.1229981  0.43126071
# [4,]  0.52291618 -0.2309106  0.10843898
# [5,]  0.72204682 -0.2578551 -0.82148326
# [6,]  0.03311892 -0.3358800  0.83184534
# [7,]  0.32333323 -0.3055211  0.29562900
# [8,] -1.50475918  0.2276755  0.43983899
# [9,]  0.01268471 -1.9462480  0.32276620
#[10,]  0.20580146  0.3255511  0.14762346


Share : facebook icon twitter icon
How to extract two columns from a matrix in a sequence of 6 columns in matlab

How to extract two columns from a matrix in a sequence of 6 columns in matlab


By : Frank Air
Date : March 29 2020, 07:55 AM
Hope that helps Basically I have a <96x659 double> matrix and I want to extract 1st and 2nd column , then 8th and 9th, then 15th and 16th column and so on.. So I want each 2 columns in a step of 6 . I hope I was clear enough. I'm newbie in matlab . in advance! , All you really need to do is construct the list of columns you want:
code :
columns = [1:7:size(matrix,2)+1,  2:7:size(matrix,2)+1];
submat = matrix(:, columns);
submat = matrix(:, sort(columns));
Change matrix columns and get the number of lines of a sub-matrix with all elements equals

Change matrix columns and get the number of lines of a sub-matrix with all elements equals


By : Christoph Gummersbac
Date : March 29 2020, 07:55 AM
To fix this issue The problem you mention can be known as the Largest Square Block Problem
You can find several topics related to this with solutions :
code :
typedef struct s_max
{
    t_uint16 val_max_;
    t_uint16 idx_max_;
} t_max;

t_max largest_square_block(t_uint8 *grid, const t_uint16 h, const t_uint16 w)
{
    t_uint16 up, upleft, left;
    t_uint16 val_max = 0;
    t_uint16 idx_max = 0;

    t_uint16 *counter = (t_uint16 *) malloc(w * h * sizeof(t_uint16));
    counter[0] = grid[0];

    for (t_uint i = 1; i < w * h; i ++)
    {   
        up = upleft = left = 0;
        if (grid[i] != YOURMAGICNUMBER)
        {
            if (i >= w)
                up = counter[i - w];
            if (i % w > 0)
            {
                left = counter[i - 1];
                if (i >= w)
                    upleft = counter[i - 1 - w];
            }
            counter[i] = 1 + min3_uint16(up, upleft, left);
            if (counter[i] > val_max)
            {
                val_max = counter[i];
                idx_max = i;
            }

        }
    }

    free(counter);

    t_max data_max;
    data_max.val_max_ = val_max;
    data_max.idx_max_ = idx_max;

    return data_max;
}
Perform operation on grouped columns of a matrix

Perform operation on grouped columns of a matrix


By : Malcolm C
Date : March 29 2020, 07:55 AM
I hope this helps you . Suppose I have a matrix M and vector G of integers, such that G has as many entries as there are columns in M. For example (schematically): , Here is one possibility:
code :
## I know your matrix has 4 rows, that is what the `4` in the following means
lst <- split(M, rep(G, each = 4L))
sapply(lst, function (x) sapply(split(x, 1:4), max) )

#   1  2  3
#1 20 71 64
#2 88 80 86
#3 31 84 46
#4 46 76 67
perform calculation on all combinations of columns of a matrix

perform calculation on all combinations of columns of a matrix


By : WP II
Date : October 13 2020, 06:00 AM
I think the issue was by ths following , One option is to define a function to apply for two columns and then use outer to apply to all combination of columns.
code :
fun <- function(x, y) {
   cosine(mat[, x], mat[, y])
}

outer(seq_len(ncol(mat)), seq_len(ncol(mat)), Vectorize(fun))

#       [,1]   [,2]   [,3]   [,4]   [,5]  ..... 
#[1,] 1.0000 0.7824 1.0000 0.7824 1.0000 .....
#[2,] 0.7824 1.0000 0.7824 1.0000 0.7824 .....
#[3,] 1.0000 0.7824 1.0000 0.7824 1.0000 .....
#[4,] 0.7824 1.0000 0.7824 1.0000 0.7824 .....
#[5,] 1.0000 0.7824 1.0000 0.7824 1.0000 .....
#....
How to perform a calculation for all columns of a matrix?

How to perform a calculation for all columns of a matrix?


By : Mark Taylor
Date : October 03 2020, 11:00 PM
I hope this helps you . I have a matrix with tree ring width for 66 trees. Rows represent the years of measurement and columns represent the different trees. , Using base R, we can use lapply
code :
treegrowth[paste0(names(df[-1]), "_growth")] <- lapply(treegrowth[-1], 
                  function(x) c(NA, x[-1]/x[-length(x)]))

#  year tree1 tree2 tree3 tree1_growth tree2_growth tree3_growth
#1 1900  0.72  0.34  1.34           NA           NA           NA
#2 1901  0.56  0.88  0.98    0.7777778    2.5882353    0.7313433
#3 1902  1.23  0.56  1.67    2.1964286    0.6363636    1.7040816
library(dplyr)
treegrowth %>% mutate_at(-1, list(growth = ~./lag(.)))
treegrowth <- structure(list(year = 1900:1902, tree1 = c(0.72, 0.56, 1.23), 
tree2 = c(0.34, 0.88, 0.56), tree3 = c(1.34, 0.98, 1.67)), 
class = "data.frame", row.names = c(NA, -3L))
Related Posts Related Posts :
  • how to loop for division funciton in r
  • Why does ggplot not allow suppressing of messages generated by its geoms?
  • Download multiple excel files linked through urls in R
  • sparklyr : spark_apply function is not working in cluster mode
  • dplyr mutate - How do I pass one row as a function argument?
  • R selecting rows by conditions given in an external table
  • Native regex way to replace multiple leading chars with equal number spaces
  • stan - difficulty vectorizing
  • How to define a function that calls shiny functions?
  • How to count number of observations in a "n" dimensional range in R
  • Superimposing asymmetric t-distribution using ggplot2
  • Makefile to render all targets of all .Rmd files in directory
  • Authentication failure with rdrop2
  • DT data table display error
  • Issue when adding new rows (with nested dataframes within) to a dataframe
  • R-How to compare two dataframe and update list column value
  • Series vector for approximating pi
  • what is difference between "variance explained " in Random Forest and "merror" in XGBoost
  • R - Cast dataframe on unique rows - reshape2
  • ggplot2: plot correct proportions using geom_bar
  • Speedup query for R data.table - can this two-argument function be applied by group more quickly?
  • apply a function to several columns at once with mutate
  • R 'cowplot' neatly produce gridded plot with shared (common) legends and unique legends
  • Repeat R script for many times and save results to text file
  • How to negative lookbehind for special characters
  • data.table inner join produces error when no match is found
  • Create a new column base on existing column, but row above
  • Is there a way to visualize the process of source() in RStudio?
  • google places api consumes 10 request but I am doing only 1
  • Statistical mode of a categorical variable in R (using mlv)
  • Using for-loop to mutate a data.frame in r
  • Make plot with regression line for mixed model
  • Shortcut to select matces cases in R studio
  • vectoriced norm/matrix multiplication
  • Negative log10 transformation in R
  • Plot data with duplicate points
  • Visualizing crosstab tables with a plot in R - changing colours
  • How to manually modify automated numbers and labels in plot
  • How can I follow any redirections of a url in R?
  • Add jitter to box plot using markers in plotly
  • Adding an extra item to the legend
  • ggplot fills in data in the wrong order
  • Convert list to data frame
  • R: filtering by list(s) of strings and returning all results that start with the content of the lists
  • R:How to attach parts of a data frame with different headers and/or an overflowing piece of the dat frame
  • How to use 'par' for manipulating plot margins?
  • Can dplyr::case_when return mix of NAs and non-NAs?
  • Text preprocessing and topic modelling using text2vec package
  • Uploading multiple files in Shiny, process the files, rbind the results and return a download
  • R levelplot: color green-white-red (white on 0) according to one variable, but show the values of another variable
  • Why [i] doesn't point to the starting point in a vector
  • In R after generating a mvrnorm distribution, Y, what does Y[,1] do?
  • expand a data frame to have as many rows as range of two columns in original row
  • Getting started with R and CFA
  • Re order x-axis in ggplot so time goes from 12AM to 11PM in R
  • R - Automatically stack every nth column of a data frame and save them as new objects
  • How to format dplyr output in R into doubles (or other workable format)?
  • Dataframe to matrix conversion using tapply turns zeros to NAs
  • Smallest multiple of 1:20 - How can I make it quicker?
  • How to specify the size of a graph in ggplot2 independent of axis labels
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org