art.frame
Problem to Solve
You’ve learned about data frames: objects for storing data in rows and columns. In a file called art.R
in a folder called art.frame
, practice using data frames to create your very own art in R.
Demo
Background
You’ve learned how to create a data frame by reading data from a file. Turns out you can also create your own data frames from scratch, using a function called data.frame
.
Per its documentation, data.frame
can be provided any number of named arguments. Those argument names become the data frame’s column names. Then, the values for each of those arguments become the given column’s values.
Take a look at how you could create a small, 3x3 data frame:
data.frame(
A = c("1", "2", "3"),
B = c("4", "5", "6"),
C = c("7", "8", "9")
)
Notice that there are 3 named arguments: A
, B
, and C
. Each is passed a different value—a vector of 3 numbers!
Curious about c
?
c
is a function that creates a vector from a list of elements, separated by commas.
The above code produces a data frame like the below:
A | B | C | |
---|---|---|---|
1 | 1 | 4 | 7 |
2 | 2 | 5 | 8 |
3 | 3 | 6 | 9 |
Consider both widening and lengthening your data frame, as well as filling each column with a vector of empty spaces:
data.frame(
A = c(" ", " ", " ", " ", " "),
B = c(" ", " ", " ", " ", " "),
C = c(" ", " ", " ", " ", " "),
D = c(" ", " ", " ", " ", " "),
E = c(" ", " ", " ", " ", " ")
)
You now have a blank canvas with which to create your art!
Distribution Code
For this problem, you’ll need to create art.R
in a folder called art.frame
.
Create art.R
Open RStudio per the linked steps and navigate to the R console:
>
Next execute
getwd()
to print your working directory. Ensure your current working directory is where you’d like to create this problem’s folder. If using RStudio through cs50.dev the recommended directory is /workspaces/NUMBER
where NUMBER
is a number unique to your codespace.
If you do not see the right working directory, use setwd
to change it! Try typing setwd("..")
if in the working directory of another problem, which will move you one directory higher.
Next execute
dir.create("art.frame")
in order to create a folder called art.frame
in your codespace.
Now type
setwd("art.frame")
followed by Enter to move yourself into (i.e., open) that directory. Your working directory should now end with
art.frame/
Finally, type
file.create("art.R")
to create a file called art.R
inside of the art.frame
folder.
If all was successful, you should execute
list.files()
and see art.R
. If not, retrace your steps and see if you can determine where you went wrong!
To start on this problem, copy and paste the below code into art.R
:
art <- data.frame(
A = c(" ", " ", " ", " ", " "),
B = c(" ", " ", " ", " ", " "),
C = c(" ", " ", " ", " ", " "),
D = c(" ", " ", " ", " ", " "),
E = c(" ", " ", " ", " ", " ")
)
print(art)
Specification
Modify the data frame stored as art
to make art of your choice. You have creative freedom to:
- Add rows and columns
- Rename columns
Consider avoiding emoji: when printed, they appear wider than other characters and will break the vertical alignment of your columns. 🙁
The only specifications you must adhere to are the following:
- You should store your data frame using an object called
art
. - You should print your data frame at the end of your program.
- Your data frame should contain at least 3 rows and 3 columns.
Usage
Assuming art.R
is in your working directory, enter the below in the R console to test your program:
source("art.R")
How to Test
Run your program frequently, making modifications to your art as you go.
check50
You can also check your code using check50
, a program that CS50 will use to test your code when you submit. But be sure to test it yourself as well!
Run the following command in the RStudio console:
check50("cs50/problems/2024/r/art.frame")
Green smilies mean your program has passed a test! Red frownies will indicate your program output something unexpected. Visit the URL that check50 outputs to see the input check50 handed to your program, what output it expected, and what output your program actually gave.
How to Submit
You can submit your code using submit50
.
Keeping in mind the course’s policy on academic honesty, run the following command in the RStudio console:
submit50("cs50/problems/2024/r/art.frame")