Question

In: Computer Science

Why does this code fail? How can you fix it.** ```{r} table4a %>% gather(1999,2000,key="year",value="cases") ``` Answer:...

Why does this code fail? How can you fix it.**
```{r}
table4a %>% gather(1999,2000,key="year",value="cases")
```

Answer:

**2) Tidy the simple tibble below. Do you need to spread or gather it? What are the variables?**
```{r}
preg <-tribble(~pregnant, ~male, ~female,
"yes", NA,10,
"no" ,20,12)

preg
```


**3) What do the extra and fill arguments do in separate()? Experiment with the various options for the following two toy datasets?**
```{r}
tibble(x=c("a,b,c","d,e,f,g","h,i,j"))%>% separate(x,c("one","two","three"))

tibble(x=c("a,b,c","d,e","f,g,i"))%>%separate(x,c("one","two","three"))
```

**4) Both unite and separate have a remove argument. What does it do? Why would you set it to FASLE?**


Solve all please with explanations

Solutions

Expert Solution

Q1) Why does this code fail? How can you fix it.
```{r}
table4a %>% gather(1999,2000,key="year",value="cases")
```
Answer:---
Because `gather` can't find the columns names. You can't name columns w/ numbers in R without quoting them with tick marks.

Q2) Tidy the simple tibble below. Do you need to spread or gather it? What are the variables?**
```{r}
preg <-tribble(~pregnant, ~male, ~female,
"yes", NA,10,
"no" ,20,12)

preg
```
Answer:--------
The main objective of analysis here is whether pregnant or not (bc males can not be pregnant), so I would go for `gather`ing the gender column rather than spreading the pregnant column.

```{r}
preg %>%
gather(gender, values, -pregnant)
# the other way around:
preg %>%
gather(gender, values, -pregnant) %>%
spread(pregnant, values)
```

Q3) What do the extra and fill arguments do in separate()? Experiment with the various options for the following two toy datasets?**
```{r}
tibble(x=c("a,b,c","d,e,f,g","h,i,j"))%>% separate(x,c("one","two","three"))

tibble(x=c("a,b,c","d,e","f,g,i"))%>%separate(x,c("one","two","three"))
```

Answer:-----
It's simple. x has vectors with 3 and 4 characters but we specify 3 columns. `fill` has three values:
`warn`, `right` and `left`. Here I specify a fourth column to place the extra letter. The first fills the missing values with the extra character using the right most match. `left` does the same thing but without a warning. and left places the extra character empty in the first column

```{r}
tibble(x = c("a,b,c", "d,e,f,g", "h,i,j")) %>%
separate(x, c("one", "two", "three", "four"))

tibble(x = c("a,b,c", "d,e,f,g", "h,i,j")) %>%
separate(x, c("one", "two", "three", "four"), fill = "right")

tibble(x = c("a,b,c", "d,e,f,g", "h,i,j")) %>%
separate(x, c("one", "two", "three", "four"), fill = "left")
```
I've deleted the fourth column to see how this works. `extra` on the other hand, deals with either droping or merging the extra characters. `warn` drops the extra character and emits a warning messge.
`drop` does the same thing but without a warning and `merge` merges the extra character to it's closest end. No aparent option to `merge` with the first column rather than the last.

```{r}
tibble(x = c("a,b,c", "d,e,f,g", "h,i,j")) %>%
separate(x, c("one", "two", "three"), extra = "warn")

tibble(x = c("a,b,c", "d,e,f,g", "h,i,j")) %>%
separate(x, c("one", "two", "three"), extra = "drop")

tibble(x = c("a,b,c", "d,e,f,g", "h,i,j")) %>%
separate(x, c("one", "two", "three"), extra = "merge")

Q4) Both unite and separate have a remove argument. What does it do? Why would you set it to FASLE?**
Answer:-------

Because `unite` and `separate` receive columns and create new ones, `remove` allows you to remove the original columns that you unite/separate on. You might want to leave them as they are if you're checking whether the transformation was done correctly.


Related Solutions

Python 3 Fix the code so if the user does not select a value in the...
Python 3 Fix the code so if the user does not select a value in the sold by field, it shows a warning message indicating "Choose one value" import tkinter as tk from tkcalendar import DateEntry from openpyxl import load_workbook window = tk.Tk() window.title("daily logs") window.grid_columnconfigure(1,weight=1) window.grid_rowconfigure(1,weight=1) # labels tk.Label(window, text="Bar code").grid(row=0, sticky="W", pady=20, padx=20) tk.Label(window, text="Products failed").grid(row=1, sticky="W", pady=20, padx=20) tk.Label(window, text="Money Lost").grid(row=2, sticky="W", pady=20, padx=20) tk.Label(window, text="sold by").grid(row=3, sticky="W", pady=20, padx=20) tk.Label(window, text="Failed date").grid(row=4, sticky="W", pady=20, padx=20) #...
Can you fix the code and comment the fix Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -39 at...
Can you fix the code and comment the fix Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -39 at CaesarCipher.encrypt(CaesarCipher.java:28) at CaesarCipher.main(CaesarCipher.java:52) public class CaesarCipher{     char[] encoder = new char[52];     char[] decoder = new char[52];      public CaesarCipher(int rotation)      {        for(int k=0 ; k < 26 ; k++)        {            encoder[k] = (char) ('A' + (k + rotation) % 26);            decoder[k] = (char) ('A' + (k - rotation + 26) % 26);        }        for(int j...
Can you fix the errors in this code? package demo; /** * * */ import java.util.Scanner;...
Can you fix the errors in this code? package demo; /** * * */ import java.util.Scanner; public class Booolean0p {        public class BooleanOp {            public static void main(String[] args) {                int a = 0, b = 0 , c = 0;                Scanner kbd = new Scanner(System.in);                System.out.print("Input the first number: ");                a = kbd.nextInt();                System.out.print("Input...
11. What are some key properties of a crystal. How does this allow us to gather...
11. What are some key properties of a crystal. How does this allow us to gather 3D information about proteins? (500 words)
Can you fix the errors in this code? import java.util.Scanner; public class Errors6 {    public...
Can you fix the errors in this code? import java.util.Scanner; public class Errors6 {    public static void main(String[] args) {        System.out.println("This program will ask the user for three sets of two numbers and will calculate the average of each set.");        Scanner input = new Scanner(System.in);        int n1, n2;        System.out.print("Please enter the first number: ");        n1 = input.nextInt();        System.out.print("Please enter the second number: ");        n2 =...
Can you fix this code please. the removing methods id no doing anything. this is java...
Can you fix this code please. the removing methods id no doing anything. this is java code import java.util.NoSuchElementException; public class DoublyLinkedList<E> {    public int size;    public Node head;    public Node tail;             @Override    public boolean isEmpty() {               return size == 0;    }    @Override    public int getSize() {               return 0;    }    @Override    public void addAtFront(E element) {       ...
How does the “too big to fail” increase moral hazard? Can you cite an example from...
How does the “too big to fail” increase moral hazard? Can you cite an example from the 2007- 2009 financial crisis?
Imagine that you are a sales rep for a major insurance company. How can you gather...
Imagine that you are a sales rep for a major insurance company. How can you gather customer feedback to improve your service? How can you use customer feedback that you receive about products and services for which you are not responsible? Please explain in detail
Why my net pay is always 0, and how can I fix it ? here's the...
Why my net pay is always 0, and how can I fix it ? here's the code #include <iostream> #include <fstream> #include <iomanip> #include <cmath> using namespace std; int main() { ofstream out; out.open("myData.txt"); string fname;    cout << "name?" << endl; cin >> fname; double salary; cout << "salary?" << endl; cin >> salary;    double fedTax = salary * 15 / 100; double stateTax = salary* 3.5 / 100; double SST = salary * 5.75 / 100; double...
Q: Why is modern-day Japan experiencing a decrease in birthrate and how can they fix it?...
Q: Why is modern-day Japan experiencing a decrease in birthrate and how can they fix it? Explain in 300 words. • Introduction Provide a brief introduction to the issue or problem. This is important if the general public does not have much understanding to give them background. For your thesis (opinion or point of view), state what you think needs to change or be done. • Point1: Acknowledge opposing arguments Include a section that explains the argument in opposition to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT