```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

This challenge covers topics covered in the first **three workshops** of the basic coding track. 

This challenge has been adapted from [Columbia University Psychology Department's Scientific Computing Workshops](https://cu-psych-computing.github.io/cu-psych-comp-tutorial/){target="_blank"} and some materials provided by Ana DiGiovanni and Anna Vannucci. 

For each of the following questions, the answer should be expressed as the output of a command or a series of commands.

# Programming fundamentals

1. Show the file path to R's current working directory on your computer.

```{r}
getwd()
```

2. What is 75 cubed? 
```{r}
75 ^ 3
```

3.	What is the square root of 600? 
HINT: Look up the following on Google- " R square root function"

```{r}
sqrt(600)
```

4.	a) Create two vectors with the following elements: "vector_1" with elements 4, 8, 2, 9 and "vector_2" with elements 1, 2, 2, 3. 
b) Multiply vector_1 and vector_2 and store the result in variable y
 
```{r}
vector_1 <- c(4,8,2,9)
vector_2 <- c(1,2,2,3)
y <- vector_1 * vector_2
y

```

Run this line of code
```{r}
fav_veggies <- c("carrot", "parsley","lettuce", "kale", "green pepper","cucumber") #these are my guinea pig's favorite veggies
```
  
5. Output elements 3 through 5 of the vector fav_veggies
```{r}
fav_veggies[3:5]
#OR
fav_veggies[c(3,4,5)]

```

6. Is "parsley" one of my guinea pig's favorite veggies? Is "red pepper" one of my guinea pig's favorite veggies?
HINT: Look up relational operators from Lesson 2
```{r}
"parsley" %in% fav_veggies
"red pepper" %in% fav_veggies

```

7. Create a variable "is_green" storing a logical vector of length 6, where the nth element is `TRUE` if the nth element of fav_veggies is green, and `FALSE` if it isn't green. For e.g. Carrot would be `FALSE` since it isn't green, but kale would be `TRUE`.

```{r}
is_green <- c(FALSE, TRUE, TRUE, TRUE, TRUE, TRUE)

```

8. Output all of the green veggies in the fav_veggies vector to console. 
HINT 1: you can index a vector with a logical vector, and it will only return the elements that correspond to a "TRUE" value.
HINT 2: Try indexing the fav_veggies vector using is_green.

```{r}
fav_veggies[is_green]

```

Run the following chunk of code 
```{r}
#I have two guinea pigs: tuco and zuko
# these are the weights of my guinea pigs in lbs (pounds)
tuco_wt <- 2.4
zuko_wt <- 1.8

# these are the ages of my guinea pigs (in yrs)
tuco_age <- 1.5
zuko_age <- 0.9
```

9. According to the vet, I can feed the guinea pig baby food if they are below the age of 1 and if they are greater than 1.5 lbs. Can I feed them baby food? Save the results in a variable as `TRUE` (can give baby food) or `FALSE` (cannot give baby food) for the two of them. HINT: Do this using logical and relational operators (not just looking at the numbers)! 

```{r}
# can I give tuco baby food?
tuco_baby_food <- tuco_age < 1 & tuco_wt > 1.5
tuco_baby_food

```

```{r}
# can I give zuko baby food? 
zuko_baby_food <- zuko_age < 1 & zuko_wt > 1.5
zuko_baby_food
```


# Data frames and data cleaning

## Read in data

1. Read in the data from the csv file `Study2_Subjects.csv` and save it in a variable named `study2`. Remember to load any necessary packages first. 

```{r}
library(tidyverse)
study2 <- read_csv("Study2_Subjects.csv")
```

2. View the first 5 rows of the dataframe and check the datatype for all the columns

```{r}
# View the first 5 rows of the data frame
head(study2, 5)
```

3. How many columns do you have in the dataset? What are their names?
```{r}
ncol(study2)
names(study2)
```


4. How many observations/subjects do you have in the dataset?
```{r}
nrow(study2)
```
5. What is the mean age of participants in your dataset? 
```{r}
mean(study2$Age)
```
6. What is the median score for depression symptoms in the personality questionnaire?
HINT: Look at the readme file to see which column stores the depression symptoms score.
```{r}
median(study2$Q2)
```

7. Check for  for missing values

```{r}
study2 %>%
  filter(!complete.cases(.))
```


8. We can actually fix this missing value, because we have this information in the README. Check the README and fix the missing value.
```{r}
# According to the readme, Subject ID 9 was in Condition C, so we need to fill the missing value with the condition
study2$Condition[study2$ID==9] <- "C"

# now check that the missing value has been replaced
study2 %>%
  filter(!complete.cases(.))

```

9. The columns Q1, Q2 and Q3 are not descriptive enough. Just looking at the columns we don't know what they refer to. Check the README file to get more info, and rename the columns in a more informative way. Remember to store it in a new variable (e.g. `study2_cleaned`)
HINT: Use the `rename()` function


```{r}
study2_cleaned <- study2 %>%
  rename("Anxiety" = "Q1" , "Depression" = "Q2", "Stress" = "Q3")
```

10. Moving forward, we don't really care about the age and the sex. We are more interested in the questionnaire scores and the condition. Select only the variables you care about. 

HINT: use the function `select()` 
HINT 2: Be sure to keep the Subject ID in the dataframe, because we need that for future analyses.

```{r}
# I'm reading in the cleaned dataframe and storing it again in study2_cleaned
study2_cleaned <- study2_cleaned %>%
  select(-c("Age", "Sex"))
```

The use of `%>%` is especially important for stacking functions. So we could do step 9 and 10 together as one.
```{r}
# study2_cleaned <- study2 %>%
#   rename("Anxiety" = "Q1" , "Depression" = "Q2", "Stress" = "Q3") %>%
#   select(-c("Age", "Sex"))
```

11. Now filter for those Subjects who are in condition B only. Don't save this into a variable, we only to look at the data for those in condition B.

```{r}
study2_cleaned %>%
  filter(Condition == "B")

```

12.Save this cleaned dataframe as `Study2_cleaned.csv`
```{r}
write_csv(study2_cleaned,"study2_cleaned.csv")
```
