Question

Read the data set in BlackFriday.xlsx into R. Call the loaded data BlackFriday.xlxs. Make sure that...

Read the data set in BlackFriday.xlsx into R. Call the loaded data BlackFriday.xlxs. Make sure that you have the directory set to the correct location for the data.
Find the average of purchase amount using for loop.
Find the average of purchase amount using while loop.
Find the average of purchase amount using repeat loop.
Find the average of purchase amount for female shoppers using for loop.
Find the average of purchase amount for female shoppers using while loop.
Find the average of purchase amount for female shoppers using repeat loop.
Find the differences between the average of purchase amount for female and male shoppers.

large file, I just need the R scrpit

User_ID Product_ID Gender Age Occupation City_Category Stay_In_Current_City_Years Marital_Status Product_Category_1 Product_Category_2 Product_Category_3 Purchase

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Use below statements to the path if your dataset isn't in the same folder as your executable

# Returns the current directory

getwd()

# set the path to required directory

setwd("Path to dataset")

# read the table into dataframe named df

# also,add an attribute header = true, if your data has heading in first column

df <- read.table("BlackFriday.xlsx")

****** average using for loop

sum = 0

for(each in df[["Purchase"]])

{

sum = sum + each

}

Average = sum / nrow(df)

****** average using while loop

sum = 0

i = nrow(df)

j = 0

while( j < i )

{

sum = sum + df[j,Purchase]

j = j +1

}

Average = sum / nrow(df)

****** average using repeat loop

sum = 0

i = nrow(df)

j = 0

repeat( j < i )

{

sum = sum + df[j,Purchase]

j = j + 1

if (j == i )

{

break

}

}

Average = sum / nrow(df)

****** average using while loop for female customers

sum = 0

i = nrow(df)

j = 0

count = 0

while( j < i )

{

if (df[j,Gender] == "Female")

{

sum = sum + df[j,Purchase]

count = count + 1

}

j = j +1

}

Average = sum / count

****** average using repeat loop

sum = 0

i = nrow(df)

count = 0

j = 0

repeat( j < i )

{

if (df[j,Gender] == "Female")

{

sum = sum + df[j,Purchase]

count = count + 1

}

j = j + 1

if (j == i )

{

break

}

}

Average = sum / count

****** average difference between male and female using while loop

male_sum = 0

female_sum = 0

i = nrow(df)

j = 0

male_count = 0

female_count = 0

while( j < i )

{

if (df[j,Gender] == "Female")

{

female_sum = female_sum + df[j,Purchase]

female_count = female_count + 1

}

if (df[j,Gender] == "Male")

{

male_sum = male_sum + df[j,Purchase]

male_count = male_count + 1

}

j = j +1

}

Average difference = ( female_sum / female_count ) -   ( male_sum / male_count )

If you don't understand, please post your question in comments.

Add a comment
Know the answer?
Add Answer to:
Read the data set in BlackFriday.xlsx into R. Call the loaded data BlackFriday.xlxs. Make sure that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Is there a way to make a pivot table from a data set to show the following: - make gender the columns (one column for male and one for female) - rows are age increments (18 - 30, 31 - 40, 41 - 50, 51...

    Is there a way to make a pivot table from a data set to show the following: - make gender the columns (one column for male and one for female) - rows are age increments (18 - 30, 31 - 40, 41 - 50, 51 - 60, 61 - 70) - information provided within the pivot table is the average salary of everyone within the age increment (for example, I want to find the average salary of a male between...

  • If a person from your data set is randomly selected, find the probability that they are...

    If a person from your data set is randomly selected, find the probability that they are left-handed or male. L=ledt, R=Right, 1=Female, 2=male Tabulated Statistics: gender, handedness Rows: gender Colums: handedness L R Missing All 2 Missing 18 248 15 114 2 0 266 1 12에

  • Using R, code the following: The data set `normtemp` (**UsingR**) contains body measurements for 130 healthy,...

    Using R, code the following: The data set `normtemp` (**UsingR**) contains body measurements for 130 healthy, randomly selected individuals. The variable temperature contains normal body temperature data and the variable gender contains gender information, with male coded as 1 and female coded as 2. First, split the data by 'gender'. If the conditions for a two-sample t-test are met then perform the correct t-test to test if the population means (by gender) are equal. Make sure you run the correct...

  • You are given SmallSample.csv data. Please complete the following using this data and write a rscript....

    You are given SmallSample.csv data. Please complete the following using this data and write a rscript. Please read SmallSample.csv data and create a data frame variable call smallsample. Please show the first six records to check data frame format Please show the structure of the data. Please check every column’s class: age& income should be numeric; gender, marital, and risk should be factor. numkids (number of kids) should be integer. If they are not the correct type, please change them...

  • [USING RSTUDIO] I am having trouble already in step 3 of number 1. Im not sure...

    [USING RSTUDIO] I am having trouble already in step 3 of number 1. Im not sure why what I typed isn't working. I appreciate anybody that can help me out. Thanks 1. Recall the `iris` data set from last week's exercise. The `iris` data set is already pre-loaded in R - look at the help file using `?iris` for more information on this data set.    i) Check the structure of the data using the function `str(iris)`.    ii) Find...

  • The data set consists of information on 3800 full-time fll-erworkers. The highest educational achievement for each...

    The data set consists of information on 3800 full-time fll-erworkers. The highest educational achievement for each worker was either a high school diploma or a bachelors degree. The workers ages ranged from 25 to 45 years. The data set also contained information on the region of the country where the person lived, marital status, and number of children. For the purposes of these exercises, let AHEaverage hourly earnings (in 2005 dollars) Collegebinary variable (1 if college, O if high school)...

  • The data set consists of information on 3700 full-time full-year workers. The highest educational achievement for...

    The data set consists of information on 3700 full-time full-year workers. The highest educational achievement for each worker was either a high school diploma or a bachelor's degree. The worker's ages ranged from 25 to 45 years. The data set also contained information on the region of the country where the person lived, marital status, and number of children. For the purposes of these exercises, let AHE = average hourly earnings (in 2005 dollars) College = binary variable (1 if...

  • The data set consists of information on 4900 full-time full-year workers. The highest educational achievement for...

    The data set consists of information on 4900 full-time full-year workers. The highest educational achievement for each worker was either a high school diploma or a bachelor's degree. The worker's ages ranged from 25 to 45 years. The data set also contained information on the region of the country where the person lived, marital status, and number of children. For the purposes of these exercises, let AHE = average hourly earnings (in 2005 dollars) College = binary variable (1 if...

  • The following data set shows the ages of the Best Actress and Best Actor award at...

    The following data set shows the ages of the Best Actress and Best Actor award at a given awards show for various years: Actress Age Actor Age 38 38 33 52 39 53 32 52 32 67 39 55 24 54 25 66 25 55 22 31 36 64 24 49 39 39 25 45 28 57 Using a Sign Test, test the claim that there is no median difference between the ages of Best Actress and Best Actor award...

  • Data manipulation using R Download and load Most_popular_baby_name.csv to R using the following R code: library('tidyverse')...

    Data manipulation using R Download and load Most_popular_baby_name.csv to R using the following R code: library('tidyverse') baby_names <- read_csv("http://personal.stevens.edu/~fmai/data/Most_Popular_Baby_Names.csv") The file contains the counts of baby names by sex and mother's ethnicity in NYC in 2011-2014. For example, the first record indicates that in 2011, when the mother is Hispanic and the baby is female, 13 were named GERALDINE. BRTH_YR Gender ETHCTY Name Count 2011 FEMALE HISPANIC GERALDINE 13 Only analyze the dataset for the years 2012 - 2014, so...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT