How to create a percentage column based on the values present in every third row?

Silent_bliss

I have a data frame containing the values of weight. I have a create a new column, percentage change of weight wherein the denominator takes the value of every third row.

df <- data.frame(weight = c(30,30,109,30,309,10,20,20,14))

# expected output
change_of_weight = c(30/109, 30/109, 109/109, 30/10,309/10,10/10,20/14,20/14,14/14)
jay.sf

Subset weight column where it's position %% 3 is zero and repeat each value three times.

df <- transform(df, change_of_weight=weight / rep(weight[1:nrow(df) %% 3 == 0], each=3))
df
  weight change_of_weight
1     30        0.2752294
2     30        0.2752294
3    109        1.0000000
4     30        3.0000000
5    309       30.9000000
6     10        1.0000000
7     20        1.4285714
8     20        1.4285714
9     14        1.0000000

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to count row in every column based on threshold

copying values to the next row in time based on a third column in r

How to calculate the a third column based on the values of two previous columns?

Percentage based column / row span

How to create duplicate rows based on a column values

How to create column based on datetime values?

How do I create a third column based on Character Values of other columns, excluding NA and values?

Conditionally Create New Column Based on Row Values

How to repalce column values using pandas , based on the words present in the cell

How to create a new variable (column) based on a combination of row values in R?

How to create a column based on values in the rows below the row in a different column

How to create a percentage column by slicing every item with its relative third item?

How to create a column of the last/right-most non-empty values in every row from a whole range

Check which values, if any, are present in every column

How to find values based on row and column data

Create and classify a row based on column values

How to group 50 columns for every row in sas and create a new column for their values?

How to create new rows in dataset based on multiple values present in array in one column of the dataset

Create new column based on row values scala spark

Create new column in DataFrame based on comparison against row values

Pandas- Create column based on sum of previous row values

How to create new columns based on the values of a different column and count the percentage value of another numerical column in R?

PySpark how to create a column based on rows values

Create column based on distinctive sets of multiple row values

How do i count values in multiple columns based on multiple criteria and create a new column row wise?

Pandas create new column based on previous and after row values

Mutate a new column based on observations present in a row

How to create a third column based on first two column in pandas dataframe?

Adding new column for different rows based on the values present in the same row for a different column