215 questions
7 votes
3 answers
106 views
How to calculate row sums in R with dynamic columns
I have the Dataframe: df <- tibble(value_2024 = runif(4), value_2023 = runif(4), value_2022 = runif(4), selection = c(2023, 2022, 2024, 2022)) I want to rowwise calculate the sum of the columns ...
0 votes
1 answer
161 views
How to calculate column sum of column values based on condition from another column
I have dataframe Category = c("Fixed","New" ,"Fixed" ,"Regular", "Fixed", "New", "New") Day1 = c(313, NA,239, 341, 14,11,521) Day2=...
1 vote
3 answers
100 views
sum across columns based on range given in another text column in r
i want to create new column for row total based on column range given in text column (col_range). I tried the following and it throws me an error. Does anybody know how to do this. d = data.frame(...
1 vote
1 answer
91 views
rowSums over different columns for each row
I would like to perform a rowSums() over specified columns. My trouble is that the desired columns to sum over are different for each row and given by another variable. The motivation is to check ...
0 votes
3 answers
112 views
Row sums by groups through index in columns
I am running R in Rstudio. I have a data frame that has the following columns: y0, y1, y2, y3, .... yn and yb0, yb1, yb2, yb3... ybn where n could be as large as 1000. I want to do row sums such ...
1 vote
2 answers
62 views
Manipulate of an array in RStudio, specific column and rowSums
In RStudio, I have marray_1. I want to manipulate the array to calculate the sum of column j divided by the sum of specific rows, i and starting at the selected column, j. For example, for the follow ...
0 votes
2 answers
1k views
Create a new column with the sum of the values from some columns in a tibble on R
I have a tibble like Table1, and I want to group some columns by the sum of their values for every row, creating a new column with the results and replacing the summed columns, like Table2. Table1 | ...
0 votes
2 answers
48 views
compare each cell in one dataframe to all values within a column in a second data frame and count the number of times the first df cell recedes in r
Would like to count the number of times a value in the whole column of DF2(DF2$B) recedes below each cell in DF1$A. Example: DF1 <- data.frame(Q=c("fun","fun"), ...
3 votes
3 answers
84 views
Generating random dataframes (5 rows 9 cols) with each rowsum should be 9
I'm trying to create 10 or more of pseudo dataframes. The data frame dim should be 9 columns with 5 rows(Mon, Tue,Wed, Thur, Fri), and each rowsum should be 9. like below. Factor1 Factor2 ...
2 votes
2 answers
89 views
Summing rows with a dynamic column range specified by another value in that row
I am working with a set of binary data for months of the year similar to the sample tibble below but with tens of thousands of rows. I am trying to add a column to the existing table that sums each ...
2 votes
5 answers
883 views
Create multiple columns with R dplyr mutate with across instead of with a loop?
I am trying to use R's dplyr package to create multiple new columns for each year in my dataset that is the sum of the columns corresponding to each year's end of quarter figures (Mar, Jun, Sep, Dec). ...
2 votes
3 answers
393 views
rowsums of specific columns filling a condition
I'm trying to sum specific columns of my data that fills some condition, like the example underneath library(dplyr) library(readr) set.seed(123) data=data.frame(id=1:4, v1=sample(c(&...
0 votes
2 answers
186 views
How to compare values in two columns and if values are equal keep as is, but if values are different, sum by row
Using R, I would like to compare values in two columns, if the values are equal I would like to keep them as is, if the values are different, I would like to sum the values in the two columns. This ...
1 vote
2 answers
101 views
Keep the rows where the rowsum of at least one of the predefined subsets of the columns is greater than a threshold
I have a dataframe like this: df <- data.frame( sample1 = c(0, 1, 2, 0, 2, 1), sample2 = c(0.3, 3, 2, 0.4, 2, 3), sample3 = c(0.2, 1, 3, 0.1, 3, 3), sample4 = c(0.4, 2, 4, 0.3, 1, 1), ...
0 votes
2 answers
322 views
Generate an m*n matrix with 1 and 0 (No identity)
I need to generate an m*n matrix with 1 and 0 where the sum of each row is 1 and each column is greater than or equal to 1. I'm trying this code but I don't know how to limit my condition on the ...