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 anyone fix this code? The code isn't rounding the answer to the nearest whole number...
Can anyone fix this code? The code isn't rounding the answer to the nearest whole number like 2.345 should be 2 and 2.546 should be 3 but the round() function isn't working as expected. The round() function is rounding 2.546 to 2 which is incorrect since 4 and below should be rounded to 2 and 5 and above should be rounded to 3. Here is the code: #importing xlwt library to write into xls import xlwt from xlwt import Workbook...
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)
Why is this java code reaching a deadlock and how to I fix it without serializing...
Why is this java code reaching a deadlock and how to I fix it without serializing the code (i.e. I don't want to do not have the Girl authenticate the Boy then have the Boy authenticate the Girl)? import java.applet.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; // Attempt at a simple handshake. Girl pings Boy, gets confirmation. // Then Boy pings girl, get confirmation. class Monitor { String name; public Monitor (String name) { this.name = name; } public String...
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) {       ...
Can you fix my code and remove the errors? Thank you!! ^^ ////////////////////////////////////////////////////////////////////////////////////////////////////// public class StackException<T,...
Can you fix my code and remove the errors? Thank you!! ^^ ////////////////////////////////////////////////////////////////////////////////////////////////////// public class StackException<T, size> extends Throwable { private final T[] S = null ; public StackException(String s) { } public T top() throws StackException { if (isEmpty()) throw new StackException("Stack is empty."); int top = 0; return S[top]; } private boolean isEmpty() { return false; } public T pop() throws StackException { T item; if (isEmpty()) throw new StackException("Stack underflow."); int top = 0; item = S[top];...
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?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT