Question

I need to know how to use R to count certain values in a variable. Example...

I need to know how to use R to count certain values in a variable. Example design code to look in a table for the variable Cars and count in the rows if they are suburu or ford out of a sample of 15.

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

#This is just example to introduce you about the concept of data.table and count in r. Actual answer to your question after this section of coding

#Example to illustrate data table in R
#For installing "data.table", use install.packages("data.table")

#library "data.table" is used to create datatable
library(data.table)

#using github repository of cars data
#reading a file
DT <- fread('https://raw.githubusercontent.com/selva86/datasets/master/Cars93_miss.csv')

#initially the cars column is named as "Manufacturer", now I am changing it into "Cars"
print(head(setnames(DT, old = "Manufacturer", new = "Cars")[]))

#Now, let us access "Cars" column and see what are the cars present
print(DT$Cars)

#Counting the frequency of each value of "Cars" column or variable
output <- DT[, .(.N), by = .(Cars)]
print(output)

Code:

Output:

ANSWER:

#Actual answer to your question

#Example to illustrate data table in R
#For installing "data.table", use install.packages("data.table")

#library "data.table" is used to create datatable
library(data.table)

#creating a vector of "Cars" that consits various cars, "topspeed" contains respective TopSpeeds
cars <- c("Audi","Audi","BMW","Chevrolet","Chevrolet","Dodge","Ford","Ford","Ford","Honda","Mazda","Suburu","Suburu","Suburu","Suburu")
topspeed <- c("240","240","195","200","200","220","240","240","240","180","180","260","260","260","260")

#Creating data table that consists cars and their respective topspeeds
dt <- data.table(Cars=cars,TopSpeed=topspeed)
print(dt)

#finding total Cars elements frequency
output <- DT[, .(.N), by = .(Cars)]

#Accessing and finding the count of Ford and suburu
nrow(dt[dt$Cars == "Ford",])
nrow(dt[dt$Cars == "Suburu",])

Code:

Output:

Add a comment
Know the answer?
Add Answer to:
I need to know how to use R to count certain values in a variable. Example...
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
  • I am working on a tutorial r question set where I need to create a "new"...

    I am working on a tutorial r question set where I need to create a "new" variable that characterizes "old" in the following manner. Define a tract to have a low rate, less than the median for the data set, medium rate between medium and 75 percentile, and high if the rate is higher than 75 percentile. What would the r code look like? Not sure how to pull "old" variable out of the data set and define a high,...

  • how can i extract specific value and strings variable in R CODE for example what if...

    how can i extract specific value and strings variable in R CODE for example what if i want to extract the study time of TDU STUDNETS students = c("TDU","CSD","FSD"," ASD") study time=c(13, 11.2, 5.4, 16.0)

  • I need to know the code to do this question in R thanks 10. (8 marks)...

    I need to know the code to do this question in R thanks 10. (8 marks) (using dataset: "meap93", in R: data(meap93, package-'wooldridge)) We want to explore the relationship between the math pass rate (scil/) and average teachers' compensation (salary + benefits) in the school (totcomp) In the population model: scill,-β0 + βι log(totcompi-u, prove that β/ 10 is the percentage point change in scill given a 10% increase in totcom. ii Use the data in MEAP93 to estimate the...

  • I need python help .. I need to know if a user were to input 3...

    I need python help .. I need to know if a user were to input 3 commands to a program and run it such as specify: Usage: ./filemaker INPUTCOMMANDFILE OUTPUTFILE RECORDCOUNT (./filemaker is the program)( (INPUTCOOMANDFUILE= a text file that contains key words that need to be searched through then decided what to do ) (RECORDCOUNT= number) Given an input file that contains the following: That specifies the text, then the output, and the number of times to be repeated...

  • I know there are stuff that can't be given by you because I need to download...

    I know there are stuff that can't be given by you because I need to download pictures for example and use them but I want the code and I can add the other stuff later. Thanks. Using Open rameworks/Code Blocks, design a Memory Box Game. The game will have closed boxes and each box will contain a picture. Each box will have a match box so each picture will be used twice. The player will click a box and when...

  • Suppose I can arrange any arbitrary number of values in rows of 8 columns. For example,...

    Suppose I can arrange any arbitrary number of values in rows of 8 columns. For example, if I have 16 values, I can arrange them as follows: 3 4 6 -2 9 0.03 0.1 -0.39 84 9 5.7 -0.09 0.37 1 2 4.1 or, if I have 13 values, I am arrange them as follows: 3.2 5 10 0.31 -8 1.3 -0.03 15 5.72 93 100 12 -0.33 etc. What is the code (python) operation to determine the number of...

  • In R how do I delete rows that contain NA values My code below dosn't give...

    In R how do I delete rows that contain NA values My code below dosn't give me what I am looking for Gamma <- sqldf("SELECT DISTINCT A.Record_ID, A.Day, B.Date FROM Slot_Info as A LEFT JOIN Volume_Info as B ON A.Day = B.Date Where Date not Null ")

  • Please I need the R program code: in part b How to find the table in...

    Please I need the R program code: in part b How to find the table in R program? pulaiuit nvesligaieu wItH Fespect to the estimate of 1 In the assumed model. B. A test is to be run on a given process for the purpose of determining the effect of an independent variable X (such as process emperature) on a certain characteristic property of the finished product Y (such as density). Four observations are to be taken at each of...

  • I know that the answer to the question is D I just need to know how...

    I know that the answer to the question is D I just need to know how to work out the problem by hand. Thanks for any and all help! 7. The return on the risky portfolio is 15%. The risk-free rate as well as the investor's borrowing rate is 10%. The standard deviation of return on the risky portfolio is 20%. If the standard deviation on the complete portfolio is 25%, the expected return on the complete portfolio is A)...

  • Count Occurrences in Seven Integers Using Java Single Dimension Arrays In this assignment, you will design...

    Count Occurrences in Seven Integers Using Java Single Dimension Arrays In this assignment, you will design and code a Java console application that reads in seven integer values and prints out the number of occurrences of each value. The application uses the Java single dimension array construct to implement its functionality. Your program output should look like the sample output provided in the "Count Occurrences in Seven Integers Using Java Single Dimension Arrays Instructions" course file resource. Full instructions for...

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
ADVERTISEMENT