Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8869361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:28:04+00:00 2026-06-14T17:28:04+00:00

I have a data frame of counts by region over time. One row of

  • 0

I have a data frame of counts by region over time. One row of the data frame contains the count totals for each column. I want to convert the data frame from counts to proportions by dividing each column cell by the count total for the respective column. Some columns contain missing observations. I have done this below using nested for-loops but suspect there might be a much easier way, perhaps using lapply. I also had trouble extracting the row of count totals.

I am posting this partly because it is time I learn to use the apply family of functions and I suspect they might be useful here, and partly because I had so much trouble creating the vector of count totals and suspect using [[ would have been helpful. Thank you for any advice on writing the above code more efficiently.

my.data = read.table(text = "
state    y1970  y1980  y1990  y2000
Alaska       4      6     NA      7
Iowa        10     20     30     40
Nevada     100    100    100    100
Ohio        50     60     NA     80
total      172    195    215    238
Wyoming      8      9     10     11
", sep = "", header = TRUE)

desired.result = read.table(text = "
state         y1970       y1980       y1990       y2000
Alaska   0.02325581  0.03076923          NA  0.02941176  
Iowa     0.05813953  0.10256410  0.13953488  0.16806723  
Nevada   0.58139535  0.51282051  0.46511628  0.42016807  
Ohio     0.29069767  0.30769231          NA  0.33613445  
total    1.00000000  1.00000000  1.00000000  1.00000000  
Wyoming  0.04651163  0.04615385  0.04651163  0.04621849  
", sep = "", header = TRUE)

state  <- as.vector(unlist(my.data[, 1]))

my.totals <- as.vector(unlist(my.data[ my.data$state=='total', 2:5]))

proportions <- matrix(NA, nrow=nrow(my.data), ncol=ncol(my.data))
proportions <- as.data.frame(proportions)

for(i in 1:nrow(my.data)) {
 for(j in 1:ncol(my.data)) {

  if(j==1) proportions[i,1] <- state[i] 
  if(j> 1) proportions[i,j] <- my.data[i,j] / my.totals[j-1]

 }
}

colnames(proportions) <- names(my.data)
proportions


#     state      y1970      y1980      y1990      y2000
# 1  Alaska 0.02325581 0.03076923         NA 0.02941176
# 2    Iowa 0.05813953 0.10256410 0.13953488 0.16806723
# 3  Nevada 0.58139535 0.51282051 0.46511628 0.42016807
# 4    Ohio 0.29069767 0.30769231         NA 0.33613445
# 5   total 1.00000000 1.00000000 1.00000000 1.00000000
# 6 Wyoming 0.04651163 0.04615385 0.04651163 0.04621849
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:28:05+00:00Added an answer on June 14, 2026 at 5:28 pm

    Probably something along these lines:

    df[, -1] <- lapply( df[ , -1], function(x) x/sum(x, na.rm=TRUE) )
    

    If it were a matrix you could have just used prop.table(mat). In this case however you need to limit to working only on the numeric columns (by excluding the first one).

    Furthermore I think you need to exclude the “total” row:

     my.data[-5, -1] <- lapply( my.data[ -5 , -1], function(x){ x/sum(x, na.rm=TRUE)} )
     my.data[ -5 , ]
        state      y1970      y1980      y1990      y2000
    1  Alaska 0.02325581 0.03076923         NA 0.02941176
    2    Iowa 0.05813953 0.10256410 0.21428571 0.16806723
    3  Nevada 0.58139535 0.51282051 0.71428571 0.42016807
    4    Ohio 0.29069767 0.30769231         NA 0.33613445
    6 Wyoming 0.04651163 0.04615385 0.07142857 0.04621849
    

    ————-

    Alternate approach:

    > my.data[,-1] <-lapply( my.data[  , -1], function(x){ x/x[5] } )
    > my.data
        state      y1970      y1980      y1990      y2000
    1  Alaska 0.02325581 0.03076923         NA 0.02941176
    2    Iowa 0.05813953 0.10256410 0.13953488 0.16806723
    3  Nevada 0.58139535 0.51282051 0.46511628 0.42016807
    4    Ohio 0.29069767 0.30769231         NA 0.33613445
    5   total 1.00000000 1.00000000 1.00000000 1.00000000
    6 Wyoming 0.04651163 0.04615385 0.04651163 0.04621849
    

    This shows what prop.table will return with missing values when used on both margins and then on rows and columns separately for a very simple matrix:

    > prop.table( matrix( c( 1,2,NA, 3),2) )
         [,1] [,2]
    [1,]   NA   NA
    [2,]   NA   NA
    > prop.table( matrix( c( 1,2,NA, 3),2), 1 )
         [,1] [,2]
    [1,]   NA   NA
    [2,]  0.4  0.6
    > prop.table( matrix( c( 1,2,NA, 3),2), 2 )
              [,1] [,2]
    [1,] 0.3333333   NA
    [2,] 0.6666667   NA
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data frame like below (20,000 rows by 49 cols). Each row
I have a data.frame df and I want that every row in this df
I have a data frame consisting of the columns; name, time, count. Showing how
I have a data frame where each row is an observation concerning a pupil.
I have a data.frame of a time series of data, I would like to
Say I have a dataframe source <- data.frame(ID=c(1,2), COUNT=c(3,4)) that looks like this: ID
I have three data sources: types<-c(1,3,3) places<-list(c(1,2,3),1,c(2,3)) lookup.counts<-as.data.frame(matrix(runif(9,min=0,max=10),nrow=3,ncol=3)) assigned.places<-rep.int(0,length(types)) the numbers in the types
I have a data.frame like this: (t=structure(list(count = c(NA, 2, NA, NA, NA, 8,
So I have a data frame, say with following data: Count Amount Org Bank
Suppose I have a data frame with a column for values and another column

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.