Javier2013
Javier2013

Reputation: 503

Merging dataframes that are outputs from a for loop in r

I have a big dataframe like this (only showing the first three columns):

dataframe is called chr22_hap12

2 1 3
2 1 3 
2 1 3 
2 1 2
2 2 1
2 2 1

I would like to get the proportion of each number (ones, twos and threes in that order) for every column and store it in a dataframe.

This is what I have so far:

for (i in 1:3 ) {

  length(chr22_hap12[,i]) -> total_snps
  sum(chr22_hap12[,i]==1,na.rm=FALSE) -> counts_ancestry_1
  sum(chr22_hap12[,i]==2,na.rm=FALSE) -> counts_ancestry_2
  sum(chr22_hap12[,i]==3,na.rm=FALSE) -> counts_ancestry_3

  (counts_ancestry_1*100)/total_snps -> ancestry_1_perc
  (counts_ancestry_2*100)/total_snps -> ancestry_2_perc
  (counts_ancestry_3*100)/total_snps -> ancestry_3_perc

  haplo_df[i] = NULL

  haplo_df[i] = c(ancestry_1_perc,ancestry_2_perc,ancestry_3_perc)
  as.data.frame(haplo_df[i])
}

I get these erros: after trying to set haplo_df[i] = NULL

Error in haplo_df[i] = NULL : object 'haplo_df' not found

and after

haplo_df[i] = c(ancestry_1_perc,ancestry_2_perc,ancestry_3_perc)

Error in haplo_df[i] = c(ancestry_1_perc, ancestry_2_perc, ancestry_3_perc) : object 'haplo_df' not found

and again with as.data.frame(haplo_df[i])

object 'haplo_df' not found

My desire output should look like this:

0.00    66.66  50.0
100.00  33.33  33.33
0.00    0.00   16.66

Upvotes: 0

Views: 337

Answers (4)

Ramnath
Ramnath

Reputation: 55695

My one liner

sapply(df, function(x){prop.table(table(x))*100})

Upvotes: 1

rnso
rnso

Reputation: 24535

Try:

mydf
  V1 V2 V3
1  2  1  3
2  2  1  3
3  2  1  3
4  2  1  2
5  2  2  1
6  2  2  1


ll = list()
for(cc in 1:3) {
    dd = mydf[,cc]
    n1 = 100*length(dd[dd==1])/nrow(mydf)
    n2 = 100*length(dd[dd==2])/nrow(mydf)
    n3 = 100*length(dd[dd==3])/nrow(mydf)
    ll[[length(ll)+1]] = c(n1, n2, n3)
}
ll
[[1]]
[1]   0 100   0

[[2]]
[1] 66.66667 33.33333  0.00000

[[3]]
[1] 33.33333 16.66667 50.00000

> t(do.call(rbind, ll))
     [,1]     [,2]     [,3]
[1,]    0 66.66667 33.33333
[2,]  100 33.33333 16.66667
[3,]    0  0.00000 50.00000

Upvotes: 0

user4117977
user4117977

Reputation: 1

Here's an alternative approach.

Sample data:

set.seed(23)
y <- 1:3
df <- data.frame(a = sample(y, 10, replace = TRUE), 
                 b = sample(y, 10, replace = TRUE), 
                 c = sample(y, 10, replace = TRUE))
#df
#   a b c
#1  2 3 2
#2  1 3 1
#3  1 2 1
#4  3 1 3
#5  3 3 2
#6  2 1 3
#7  3 2 3
#8  3 2 3
#9  3 3 1
#10 3 2 3

Calculate percentages:

newdf <- as.data.frame(t(do.call(rbind, lapply(df, function(z){
  sapply(y, function(x) (sum(z == x) / length(z))*100)
}))))

#newdf
#    a   b   c
#1 0.2 0.2 0.3
#2 0.2 0.4 0.2
#3 0.6 0.4 0.5

Upvotes: 0

shadow
shadow

Reputation: 22293

You need to define the resulting matrix before the loop and then cbind the new result to that matrix.

# define the data.frame before the loop. 
haplo_df <- NULL
for (i in 1:3 ) {
  length(chr22_hap12[,i]) -> total_snps
  sum(chr22_hap12[,i]==1,na.rm=FALSE) -> counts_ancestry_1
  sum(chr22_hap12[,i]==2,na.rm=FALSE) -> counts_ancestry_2
  sum(chr22_hap12[,i]==3,na.rm=FALSE) -> counts_ancestry_3

  (counts_ancestry_1*100)/total_snps -> ancestry_1_perc
  (counts_ancestry_2*100)/total_snps -> ancestry_2_perc
  (counts_ancestry_3*100)/total_snps -> ancestry_3_perc

  # bind the new result to the existing data
  haplo_df <- cbind(haplo_df , c(ancestry_1_perc,ancestry_2_perc,ancestry_3_perc))
}
# return the result
haplo_df
##       [,1]     [,2]     [,3]
##  [1,]    0 66.66667 33.33333
##  [2,]  100 33.33333 16.66667
##  [3,]    0  0.00000 50.00000

Instead you could also just use apply and table, e.g.

apply(chr22_hap12, 2, function(x) 100*table(factor(x, levels=1:3))/length(x))
##     V1       V2       V3
##  1   0 66.66667 33.33333
##  2 100 33.33333 16.66667
##  3   0  0.00000 50.00000

Upvotes: 1

Related Questions