Saturday, June 16, 2018

Applying different functions on alternate rows of data frame

In this post, we are going to discuss how to manipulate the data frame on every alternate rows. For this purpose, we first need to load our data in a data frame. The data can be loaded either from a file using function like read.table, read.csv etc or it can be generated ,say as data frame, during processing in R itself. In this tutorial, we will be generating a dummy data frame :

df1 <- data.frame(a = letters[1:6], b = seq(100,150,by=10), stringsAsFactors=FALSE)

df1
  a   b
1 a 100
2 b 110
3 c 120
4 d 130
5 e 140
6 f 150


In this data frame I have kept 'stringsAsFactors=FALSE' otherwise you will get warning at the end of the code like this:

Warning messages:
1: In `[<-.factor`(`*tmp*`, iseq, value = 1L) :
  invalid factor level, NA generated

Coming back to the topic, we will be creating an empty data frame to store the results :


df2 <- data.frame(key=character(), value1=double(), value2=double(), stringsAsFactors=FALSE)

df2

[1] key    value1 value2
<0 rows> (or 0-length row.names)

Now, we need to create functions that can be applied on alternate rows. For presentation, I have just kept them simple:

func1 <- function(x){x - 50}
func2 <- function(x){x + 50}

Once the above ingredients are ready, we can write a loop to iterate our functions over the whole data :

for (i in 1:dim(df1)[1]){                       #iterating over all rows starting from 1 to 6.

if (i %% 2 == 0){                              #checking and processing even rows.

df2[i,1] <- df1[i,1]                             #keeping the 1st column as it is
df2[i,2] <- df1[i,2]                             #pushing lower value in 2nd column
df2[i,3] <- lapply(df1[i,2] ,func2)        #and larger value in 3rd column. Here we have applied funtion using lapply.


       }

else {                                                #processing odd numebred rows.


df2[i,1] <- df1[i,1]
df2[i,2] <- lapply(df1[i,2] ,func1)
df2[i,3] <- df1[i,2]

      }


            }

df2



Result:

  key value1 value2
1   a     50    100
2   b    110    160
3   c     70    120
4   d    130    180
5   e     90    140
6   f    150    200

So what you're waiting for? Go on ,try it!


Applying different functions on alternate rows of data frame

In this post, we are going to discuss how to manipulate the data frame on every alternate rows. For this purpose, we first need to load our...