Question

In this activity you are asked to write a R script that generates .dat file by taking binary file...

In this activity you are asked to write a R script that generates .dat file by taking binary file as an input. The output file must contain the frequency of 1's in each row and also if the value in the cell is 1 then it should get suffixed with the column index and 0's should get ignored.
Output has to look like the following:
2 0:1 1:1
4 0:1 2:1 3:1 4:1
3 1:1 3:1 4:1

5 0:1 1:1 2:1 3:1 6:1

0, 1, 2, 3, 4, 6 are the column indexes and 1's are the values in that row and the indexed column.

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

Firstly, this post will go deeper into the ways of getting data from common sources, which is often spreadsheet-like data, into R. Just like with the previous post, the focus will be on reading data into R that is different from Excel or any other type of files.

Next, the data from other sources like statistical software, databases, web scraping, etc. will be discussed.

If you want to know more about the possible steps that you might need to undertake before importing your data, go to our first post, which explains how you can prepare your data and workspace before getting your data into R.

Reading in Flat Files Into R with scan()

Besides read.table(), which was mentioned in the first post of the R data import tutorial, the scan() function can also work when handling data that is stored in simple delimited text files. Unlike the read.table() function, the scan() function returns a list or a vector, not a dataframe.

Suppose you have the following .txt document:

24 1991
21 1993
53 1962

You can read in the data (which you can download here) with the following command:

data <- scan("birth.txt")

Note that your file can also be an online data set. In that case, you just pass the URL as the first argument of the scan() function.

Alternatively, you could also read in the data into a matrix:

data <- matrix(scan("birth.txt"),
               nrow=2,
               byrow=TRUE)

Tip: if you want to do this thoroughly, you might need to specify additional arguments to make the matrix just the way you want it to be. Go to this page for more information on the matrix() function.

You can also read the columns of the input file into separate vectors:

data <- scan("age.txt",
             what = list(Age = 0,
                         Birthyear= 0),
             skip=1,
             quiet=TRUE)

Note how you first pass the (path to the) file with its extension as an argument (depending on whether you set the working directory to the folder that holds your dataset or not) and then specify the type of data to be read in, whether or not you want to skip the first line of your dataset, which character delimits the fields and if you want to print a line that says how many items have been read.

If your data can also contain other data types, you should tweak the scan() function a little bit, just like in this example:

data <- scan("age.csv",
             what = list(Age = 0,
                         Name= "",
                         Birthyear= 0),
             skip=1,
             sep=";",
             quiet=TRUE)

Tip: you can do this yourself, too! Download the text file that was used above here.

And then you can also read in the data in a data frame:

data <- data.frame(scan("age.csv",
                        what = list(Age = 0,
                                    Name = "",
                                    Birthyear= 0),
                        skip=1,
                        sep=";",
                        quiet=TRUE)

Tip: a lot of the arguments that the scan() function can take are the same as the ones that you can use with the read.table() function. That is why it's always handy to check out the documentation! Go here if you want to read up on the scan() function's arguments.

Add a comment
Know the answer?
Add Answer to:
In this activity you are asked to write a R script that generates .dat file by taking binary file...
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
  • 1. Write a short MATLAB script or function (m-file) that generates the functions (t)eu(t) for a =...

    Only calculate manually 1. Write a short MATLAB script or function (m-file) that generates the functions (t)eu(t) for a = 0.1. Plot the value of x(t). Since the e-af almost attenuates to 0 after 5T, where T1. So make your time vector from 0 to 5T would be enough to capture the signal.) 2. Using the definition of the FT (1), manually calculate the FT of the given functions in previous exercise 0O x(t)e 3. Plot the absolute values and...

  • Question 1 a. Define the following matrices in a script file (M-file), ? = ( 8...

    Question 1 a. Define the following matrices in a script file (M-file), ? = ( 8 9 10 11; 23 9 16 15 ;11 12 3 6; 1 2 8 9 ) ? = ( 2 21 7 15; 12 4 8 22; 23 9 5 13; 23 4 21 22) ℎ = (4 9 12 15) b. Add suitable lines of codes to the M-file to do the following. Each of the following points should be coded in only...

  • Main objective: Solve the n queens problems. You have to place n queens on an n...

    Main objective: Solve the n queens problems. You have to place n queens on an n × n chessboard such that no two attack each other. Important: the chessboard should be indexed starting from 1, in standard (x, y) coordinates. Thus, (4, 3) refers to the square in the 4th column and 3rd row. We have a slight twist in this assignment. We will take as input the position of one queen, and have to generate a solution with a...

  • 2. Write a script that implements the following design: In the Downloads table, the user_id and...

    2. Write a script that implements the following design: In the Downloads table, the user_id and product_id columns are the foreign keys. Create these tables in the ex schema. Create the sequences for the user_id, download_id, and product_id columns. Include a PL/SQL script to drop the table or sequence if it already exists. Include any indexes that you think are necessary. 3. Write a script that adds rows to the database that you created in exercise 2. Add two rows...

  • MATLAB Question: Write a script for the problem pictured. Please send the script, not just the...

    MATLAB Question: Write a script for the problem pictured. Please send the script, not just the printout. Make sure your script allows you to input a value for n so when prompted, in the command window, user types n value. The n value should be the f_k if k ==1 .... Please also send print outs if you can. BELOW IS A SAMPLE ANSWER THAT DID NOT PUT THE INPUT SCRIPT NEEDED FOR THIS QUESTION TO BE CORRECT. YOU CAN...

  • The first script is validate.sh. This is a simple form validation script that will be used...

    The first script is validate.sh. This is a simple form validation script that will be used to verify the inputs given. Normally, this would be done to validate input from a website or another program before entry into a database or other record storage. In this case, we will keep it simple and only focus on the input validation step. In particular, the script should prompt the user for four values: first name, last name, zip code, and email address....

  • Programming Exercise 4.9 Write a script named numberlines.py. This script creates a program listing from a...

    Programming Exercise 4.9 Write a script named numberlines.py. This script creates a program listing from a source program. This script should: Prompt the user for the names of two files. The input filename could be the name of the script itself, but be careful to use a different output filename! The script copies the lines of text from the input file to the output file, numbering each line as it goes. The line numbers should be right-justified in 4 columns,...

  • PLEASE USE MATLAB Create a single MatLab script that On a single line (Line 1), defines...

    PLEASE USE MATLAB Create a single MatLab script that On a single line (Line 1), defines matrix A such that A = 1 2 4. 5 7 8 Suppress the echo of A. On a single line (Line 2), use the size function to echo the size of A. On a single line (Line 3), use the length function to echo the length of the entire first row of A. On a single line (Line 4), use linear index notation...

  • JAVA question: Suppose we are performing a binary search on a sorted array called numbers initialized...

    JAVA question: Suppose we are performing a binary search on a sorted array called numbers initialized as follows: // index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 int[] numbers = {-30, -9, -6, -4, -2, -1, 0, 2, 4, 10, 12, 17, 22, 30}; ​ // search for the value -5 int index = binarySearch(numbers, -5); Write the indexes of the elements that would be examined by the binary search (the mid values...

  • mathlab 8. (6 points) Choose the correct answer. (0 After executing the following script file in...

    mathlab 8. (6 points) Choose the correct answer. (0 After executing the following script file in MATLAB X = (2:4; -1:1 ; 1 2 3]; y=x(2,:); size(y') the displayed result is a. 2 2 b. 31 c. 21 d. 1 3 e. none of above (11) After executing the following script file in MATLAB X = -1; y = x*sqrt(-1); х+ у the displayed result is a. error message b. d. 1-11 e. none of above (iii) The volume of...

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