Search code examples
rcountdisplaypercentagerscript

How to display the total number of a coloumn and the percentage of a coloumn


I need to display the following:

  1. Number of total listings - the list is given, from that I need to find the total number
  2. Percentage of listings - which are not reviewed : the list is given and the entries which are not reviewed are marked as NA.

Lastly, I need to add both these variables (number of total listings, percentage of listings) to one data frame and display.

What will be the most suitable R script code to use?


Solution

  • Creating a sample dataframe:

    listings <- c("Listing_1", "Listing_2", "Listing_3", "Listing_4", "Listing_5")
    reviews <- c("Good", "Very Good", "Bad", NA, "Very Bad")
    
    df <- data.frame(listings, reviews)
    

    enter image description here

    Number of listings:

    dim(df)[1]
    

    Percentage of missing reviews:

    sum(is.na(df["reviews"])) / dim(df)[1] * 100