Create a list called myList , whose first item is a numeric vector of two number
ID: 3661658 • Letter: C
Question
Create a list called myList, whose first item is a numeric vector of two numbers 3 and 9, second item is a 2 by 2 matrix (1 and 2 on the first column, 3 and 4 on the second column), third item is a character vector of 3 values “Hello”, “Hi”, and “Hey”. In addition, the name of the first item is item1, the name of the second item is item2, and the name of the third item is item3. Contents of the list are printed below.
$item1
[1] 3 9
$item2
[,1]
[,2]
[1,]
1
3
[,2]
2
4
$item3
[1] "Hello" "Hi" "Hey"
Considering myList created in question 1, write R code(s) to use two ways to retrieve the number 9.
Write R code(s) to convert myList created in question 1 to a vector called myVector. Contents of the vector are printed below.
item 11
item 12
item 21
item 22
item 23
item 24
“3”
“9”
“1”
“2”
“3”
“4”
item 31
item 32
item 33
“Hello”
“Hi”
“Hey”
Write R code(s) to remove item2 (the 2 by 2 matrix) from myListcreated in question 1. Contents of the updated myList are printed below.
$item1
[1] 3 9
$item3
[1] "Hello" "Hi" "Hey"
Write R code(s) to create the following data frame called items (note the row names and column names).
store id
item name
price
entry 1
1
milk
$1.99
entry 2
2
egg
$0.99
entry 3
3
fish
$1.99
entry 4
4
pork
$3.99
entry 5
5
beef
$4.99
Write R code(s) to create a data frame called stores with the following contents.
store id
zip
phone
entry 1
1
22222
(111)111-1111
entry 2
2
33333
(222)222-2222
Use merge to combine the data in items and stores to form the following data frame.
store id
zip
phone
item name
price
1
1
22222
(111)111-1111
milk
$1.99
2
2
33333
(222)222-2222
egg
$0.99
Use merge to combine the data in items and stores to form the following data frame.
store id
zip
phone
item name
price
1
1
22222
(111)111-1111
milk
$1.99
2
2
33333
(222)222-2222
egg
$0.99
3
3
<N/A>
<N/A>
pork
$3.99
5
5
<N/A>
<N/A>
beef
$4.99
[,1]
[,2]
[1,]
1
3
[,2]
2
4
Explanation / Answer
To create a list called myList, the following command can be used:
> item1 = c(3,9)
> item2 = c((1,2), (3,4))
> item3 = c("Hello", "Hi", "Hey")
> myList = list(item1, item2, item3)
## either the following command
> c(do.call("cbind", myList))
## or the following command can be used to convert a list to a vector of consecutive values:
unlist(myList)
To assign values to a variable in R language, use the following syntax:
> milk <- 1.99
> egg <- 0.99
> fish <- 1.99
> pork <- 3.99
> beef <- 4.99
To store the total price of all these items into a variable
called total, use the syntax:
> total <- sum(milk, egg, fish, pork, beef)
total beomes = $13.95
> rep ("Welcome to R language", times = 5)
will repeat the text "Welcome to R language" five times
> storeId <- 1
> zip1 <- 22222
> phone1 <- 1111111111
> soreId2 <- 2
> zip2 <- 33333
> phone2 <- 2222222222
## To merge two data frames, the command merge can be used:
## Create the data frames first:
> items <- data.frame(
item1 = (3, 9),
item2 = ((1,2), (3,4)),
item3 = c("Hello", "Hi", "Hey"))
## creating a data frame called Items:
> items <- data.frame(
entry1 = (1, milk, 1.99),
entry2 = (2, egg, 0.99),
entry3 = (3, fish, 1.99),
entry4 = (4, pork, 3.99),
entry5 = (5, beef, 4.99))
The alternate syntax can also be used:
df1 = stores = data.frame(storeId = c(1:2), zip = c(rep(2, 5), 33333), phone = (1111111111), (2222222222))
## merging the 2 data frames stores and items:
(storeItems <- merge(items, stores, by.x = "entry1", by.y = "storeId"))
library(sqldf)
## using the library of Structured Query Language (SQL) Data Frame (DF) = sqldf
## using inner join of 2 data frames:
## StoreItemsDF <- sqldf("SELECT storeId, entryId FROM items
JOIN df1 USING (storeId)")
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.