Home > Blockchain >  Replace column values in a df with matching index with new values in R
Replace column values in a df with matching index with new values in R

Time:02-03

I have df, containing 2 variables, df and val. df contains numbers from 1-255 and val is random numbers generated. I also have new_vals that is a vector of 255 different values.

df = (seq(1,255,by=1))
df = as.data.frame(df)
df$val = seq(0,1,length.out=255)

new_vals = (df$val 1)
new_vals=as.data.frame(new_vals)

I want to replace the value in df, where each number 1-255 in df$df corresponds to the 255 numbers in new_vals. If the index matches replace df$val with the value at each index from new_vals.

dataframe df

df       val            
    
1   0.000000000         
2   0.003937008         
3   0.007874016 

dataframe newvals (these are the values at index 1,2,3)

new_vals
<dbl>
1.000000                
1.003937                
1.007874

Expected Output of dataframe df after replacing values at matching index

df       val            
    
1   1.000000            
2   1.003937        
3   1.007874

What is the easiest way I could do this?

Edit: I realized in this example i can just replace column, but imagine df$df's order of 1-255 was randomized or have more rows

CodePudding user response:

If I'm understanding correctly, here's a way to match indices with dplyr:

library(dplyr)    

new_vals %>% 
      mutate(index = row_number()) %>% 
      left_join(df, by = c("index" = "df"), keep = T) 

Which gives us:

    new_vals index  df         val
1   1.000000     1   1 0.000000000
2   1.003937     2   2 0.003937008
3   1.007874     3   3 0.007874016

Proposed solution without the example would be:

new_vals %>% 
  mutate(index = row_number()) %>% 
  left_join(df, by = c("index" = "df"), keep = T) %>% 
  select(df, val = new_vals)

Which gives us:

     df      val
1     1 1.000000
2     2 1.003937
3     3 1.007874
4     4 1.011811
5     5 1.015748
6     6 1.019685
7     7 1.023622
8     8 1.027559
9     9 1.031496
10   10 1.035433

CodePudding user response:

If you are sure, there are df:1-255 in df, then:

df$val[which(df$df %in% c(1:255))] <- new_vals$new_vals

In addition a for loop can bring you more control and check the index accurately:

for (row in df$df) {
  df$val[df$df==row] <- new_vals$new_vals[row]
}
  •  Tags:  
  • Related