samedi 8 juin 2019

For-If loop manipulation in R

I am trying to replicate a very simple VBA code on R, to identify duplicates. My goals is to identify how many sets of duplicates in a sample set, first by reading down across rows, then reading across column. So I came up with this piece of sample:

x<-matrix(data=c("Ali","Ali","Abu","Ali","Ahmad","siti","Ali","Abu", "Ahmad", "Ali", "Abu", "Aisyah", "Khalid", "Robin","Ahmad","Ali","JOrdan","siti"), nrow=6)
x<-data.frame(x)

colnames(x)<-c("nama1","nama2","nama3")

so you would get something like this

 nama1  nama2  nama3
1   Ali    Ali Khalid
2   Ali    Abu  Robin
3   Abu  Ahmad  Ahmad
4   Ali    Ali    Ali
5 Ahmad    Abu JOrdan
6  siti Aisyah   siti

So what I want to do is essentially:

c<-0
for (i in x){
    if (x[i,1]==x[i+1,1]){
        c=c+1
        }
    print c
    }

The final output I want is to specify how many duplicates are there in each row, and subsequently do it across the column... like

 for i=1 to 10
 for j=1 to 20
        cells(i,j)="XXX"
        do this
 next j
 next i

problem is I don't know how to specify individual cell in R like eg. in VBA you can do cells(i+1,1)=cells(i,1).. and I am learning to do very simple data manipulation in R.

Any advice is welcome! Thanks

Aucun commentaire:

Enregistrer un commentaire