How to change column element based on value change in other column in R

r replace column values conditionally
r replace values in dataframe column
r assign value to column based on another column
r replace values in column
replace values in r dplyr
r replace values in dataframe from another dataframe
replace one column with another in r
r replace string in dataframe column

I have the following data:

1   2   1           
2   3   1           
3   4   1           
4   5   1           
5   6   2           
6   7   2           
7   8   2                       
9   10  3           
10  11  3           

..and I want to change an element of the second column based on a change of value of the third column, to obtain this:

1   2   1           
2   3   1           
3   4   1           
4   1   1           
5   6   2           
6   7   2           
7   5   2                       
9   10  3           
10  9   3               

So before the value in the third column changes, I want to replace the second column element of the same row with the first element of the first column that has the same third column value.

Here's a solution that uses rle and cumsum to determine the indices of the elements that need updating in column 2.

# Create data frame
df <- read.table(text = "1   2   1           
2   3   1           
3   4   1           
4   5   1           
5   6   2           
6   7   2           
7   8   2                       
9   10  3           
10  11  3    ", header = FALSE)

# Create index
ind <- cumsum(rle(df$V3)$lengths) 

# Update data frame
df[ind, 2] <- df[head(c(1, ind + 1), length(ind)), 1]

# Print result
print(df)
#>   V1 V2 V3
#> 1  1  2  1
#> 2  2  3  1
#> 3  3  4  1
#> 4  4  1  1
#> 5  5  6  2
#> 6  6  7  2
#> 7  7  5  2
#> 8  9 10  3
#> 9 10  9  3

Created on 2019-01-16 by the reprex package (v0.2.1)

7 Modifying Values, You can also remove columns from a data frame (and elements from a list) by assigning them the symbol NULL : Every other card gets the value that it already has in deck . Just describe the values that you want to change with R's notation system, then This can save you from making errors based on missing data. So I am using R and trying to change values in a data frame in one column by comparing two columns together. I have something like. Median MyPrice 10 0 20 18 20 20 30 35 15 NA And I would like to say something like

This can be accomplished using the split, unsplit, and lapply functions. We replace the last value in the second column with the first value in the first column for each group of V3 values. The split function takes the data.frame and turns it into a list of data.

unsplit(lapply(split(dat, dat$V3), 
               FUN = function(d) {d[nrow(d), 2] <- d[1,1]; d}))

  V1 V2 V3
1  1  2  1
2  2  3  1
3  3  4  1
4  4  1  1
5  5  6  2
6  6  7  2
7  7  5  2
8  9 10  3
9 10  9  3

Replacing a values from a column in another column, with R : select <- df$A_Freq < 0.5 df[select,"A_Freq"] <- df[select,"M.F"]. or maybe more elegant : df$A_Freq <- ifelse(df$A_Freq < 0.5, df$M.F,  title: “Pandas How to replace values based on Conditions” date: “2019-07-17” categories: [ Data Science, Pandas, Python, Python, Data Science ] tags: [ DataScience, Pandas, Python ] — Using these methods either you can replace a single cell or all the values of a row and column in a dataframe based on conditions . Dataframe:

One option with rleid from data.table. After converting to data.table (setDT(df)), group by the run-length-id of 'V3', replace the last element of 'V2' with the first of 'V1' and assign (:=) it to 'V2'

library(data.table)
setDT(df)[, V2 := replace(V2, .N, first(V1)), rleid(V3)]
df
#   V1 V2 V3
#1:  1  2  1
#2:  2  3  1
#3:  3  4  1
#4:  4  1  1
#5:  5  6  2
#6:  6  7  2
#7:  7  5  2
#8:  9 10  3
#9: 10  9  3
data
df <- structure(list(V1 = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 9L, 10L), V2 = c(2L, 
 3L, 4L, 5L, 6L, 7L, 8L, 10L, 11L), V3 = c(1L, 1L, 1L, 1L, 2L, 
   2L, 2L, 3L, 3L)), class = "data.frame", row.names = c(NA, -9L
  ))

How to insert values into a column based on another columns value , r/Rlanguage: We are interested in implementing R programming language for statistics and data science. Is there a way to update the column job based on id numbers? if I have 50 id If you want to then replace value in the column "jobs" for this subset you would use: And periods reference the current list element. I want to change, actually to increase by 1, values in a certain column based on criteria from another column. I have two columns, action and values. The table name is Radar Corrections, the action column is ACTION and the values column is RATINGVALUE. So when the action is UP, I want to increase the value I had in column values by 1, else

R: Extract or Replace Parts of a Data Frame, i, j, elements to extract or replace. i, j are numeric or character or, for [ only, empty. The default is to drop if only one column is left, but not to drop if only one row is left. On the other hand, rows can be added at any row after the current last row, values are coerced is a complicated one, and one that has changed during  And as we demonstrated, it isn’t hard to change column names in R. Just to be sure to think about the balance of speed vs. flexibility your want when you write your project code. Related Topics: rename column in r. This is part of our larger series about manipulating data in R. The topics most related to this one relate to cleaning up a data

How to replace a value in a data frame based on a conditional 'If , How to change the value of a variable using R programming in a all occurrences of a character in a character column in a data frame in R. I am kind of getting stuck on extracting value of one variable conditioning on another variable. For example, the following dataframe: A B. p1 1. p1 2. p3 3. p2 4. How can I get the value of A when B=3? Every time when I extracted the value of A, I got an object, not a string.

Extract or Replace Parts of a Data Frame, The default is to drop if only one column is left, but not to drop if only one row is left. On the other hand, rows can be added at any row after the current last row, and the For [ the replacement value can be a list: each element of the list is used to is a complicated one, and one that has changed during R's development. In this post we will see two different ways to create a column based on values of another column using conditional statements. First we will use NumPy’s little unknown function where to create a column in Pandas using If condition on another column’s values. Next we will use Pandas’ apply function to do the same.