简体   繁体   中英

Use dplyr to change an R dataframe from second row across multiple columns

I have a large dataframe similar to the toy dataset created below

df<-data.frame("ID"=c("A", "A", "A", "A", "A", "B", "B", "B", "B", "B"), 
'A_Frequency'=c(1,2,3,4,5,1,2,3,4,5), 'A_Axis'=c(1,2,3,4,5,1,2,3,4,5))

The dataframe consists of an ID column and a two columns A_Frequency and A_Axis. I have created a column called A_Slope and filled it using the following for loop

id1<-unique(df$ID)###########Create list of unique IDs to subset the dataframe

In this loop we calculate A_Slope value such that the values are calculated subsetting the dataframe df by unique id and then, the values are calculated from the second row to the last row, ignoring the first row in all cases

for( j in id1){
for( i in 2:nrow(df[df$ID==df$ID[df$ID%in%j],])){
df$A_Slope[df$ID==df$ID[df$ID%in%j]][i]=10*log(2, 
10)*log((df$A_Axis[df$ID==df$ID[df$ID%in%j]][i])/

(df$A_Axis[df$ID==df$ID[df$ID%in%j]][i-1]), base = 
10)/log((df$A_Frequency[df$ID==df$ID[df$ID%in%j]] 
[i])/(df$A_Frequency[df$ID==df$ID[df$ID%in%j]][i-1]),base = 10 )}}

This works well for the toy set. I have a large dataframe with multiple columns. is it possible to use dplyr to do the same using mutate.

Expected Output

        ID A_Frequency A_Axis     A_Slope
     1   A           1      1          NA
     2   A           2      2 3.010299957
     3   A           3      3 3.010299957
     4   A           4      4 3.010299957
     5   A           5      5 3.010299957
     6   B           1      1          NA
     7   B           2      2 3.010299957
     8   B           3      3 3.010299957
     9   B           4      4 3.010299957
     10  B           5      5 3.010299957

Note : the two NA values in A_Slope column can be zero also- not necessrily NA

Hopefully I have translated your code correctly.

library(dplyr)

df %>%
  group_by(ID) %>%
  mutate(A_Slope = 10 * log10(2) * log10(A_Axis/lag(A_Axis))/
                                    log10(A_Frequency/lag(A_Frequency)))


#  ID    A_Frequency A_Axis A_Slope
#  <fct>       <dbl>  <dbl>   <dbl>
# 1 A               1      1    NA   
# 2 A               2      2    3.01
# 3 A               3      3    3.01
# 4 A               4      4    3.01
# 5 A               5      5    3.01
# 6 B               1      1    NA   
# 7 B               2      2    3.01
# 8 B               3      3    3.01
# 9 B               4      4    3.01
#10 B               5      5    3.01

Some pointers to understand the code

  • log(x, 10) replaced with log10(x)
  • to get previous value ( i - 1 ) we use lag here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM