mardi 3 octobre 2017

For traversing between rows of data frames to find frequency count of a variable

How to use for loop for traversing within rows of a DataFrame.

I am writing code for to find frequency of diamonds with color 'E'. But instead, I am getting frequency count of all rows. Here is the code snippet

Code:

dataframe1<-data.frame(diamonds)

cntx<-function(){

cnt<-0

for(i in 1:nrow(dataframe1))

{

if(dataframe1$color=="E") cnt<-cnt+1

}

return(cnt)

}

Aucun commentaire:

Enregistrer un commentaire