Criando Data Frame
Criando Data Frame
output: html_document
---
Wherever data comes from, you will almost always want to store it in a
data frame object to work with it. Now, you can start creating and
exploring data frames with the code chunks in the RMD space. To
interact with the code chunk, click the green arrow in the top-right
corner of the chunk. The executed code will appear in the RMD space
and your console.
Start by installing the required package; in this case, you will want
to install `tidyverse`. If you have already installed and loaded
`tidyverse` in this session, feel free to skip the code chunks in this
step.
```{r}
install.packages("tidyverse")
```
```{r}
library(tidyverse)
```
```{r}
age <- c(, , , )
```
With these two vectors, you can create a new data frame called
`people`:
```{r}
people <- data.frame(names, age)
```
Now that you have this data frame, you can use some different
functions to inspect it.
One common function you can use to preview the data is the `head()`
function, which returns the columns and the first several rows of
data. You can check out how the `head()` function works by running the
chunk below:
```{r}
head(people)
```
```{r}
str(people)
```
```{r}
glimpse(people)
```
You can also use `colnames()` to get a list the column names in your
data set. Run the code chunk below to check out this function:
```{r}
colnames(people)
```
Now that you have a data frame, you can work with it using all of the
tools in `R`. For example, you could use `mutate()` if you wanted to
create a new variable that would capture each person's age in twenty
years. The code chunk below creates that new variable:
```{r}
mutate(people, age_in_20 = age + 20)
```
To get more familiar with creating and using data frames, use the code
chunks below to create your own custom data frame.
First, create a vector of any five different fruits. You can type
directly into the code chunk below; just place your cursor in the box
and click to type. Once you have input the fruits you want in your
data frame, run the code chunk.
```{r}
```
Now, create a new vector with a number representing your own personal
rank for each fruit. Give a 1 to the fruit you like the most, and a 5
to the fruit you like the least. Remember, the scores need to be in
the same order as the fruit above. So if your favorite fruit is last
in the list above, the score `1` needs to be in the last position in
the list below. Once you have input your rankings, run the code chunk.
```{r}
```
Finally, combine the two vectors into a data frame. You can call it
`fruit_ranks`. Edit the code chunk below and run it to create your
data frame.
```{r}
```
After you run this code chunk, it will create a data frame with your
fruits and rankings.
## Activity Wrap Up
In this activity, you learned how to create data frames, view them
with summary functions like `head()` and `glimpse()`, and then made
changes with the `mutate()` function. You can continue practicing
these skills by modifying the code chunks in the rmd file, or use this
code as a starting point in your own project console. As you explore
data frames, consider how they are similar and different to the tables
you have worked with in other data analysis tools like spreadsheets
and SQL. Data frames are one of the most basic building blocks you
will need to work with data in `R`. So understanding how to create and
work with data frames is an important first step to analyzing data.
Continuação
---
título: "Lição 2: Soluções de Dataframe"
saída: html_document
---
```{r}
instalar.pacotes("tidyverse")
```
```{r}
biblioteca(tidyverse)
```
```{r}
idade <- c(15, 19, 21, 25)
```
Com esses dois vetores, você pode criar um novo quadro de dados
chamado `pessoas`:
```{r}
pessoas <- data.frame(nomes, idade)
```
Agora que você tem esse quadro de dados, pode usar algumas funções
diferentes para inspecioná-lo.
Uma função comum que você pode usar para visualizar os dados é a
função `head()`, que retorna as colunas e as primeiras linhas de
dados. Você pode verificar como a função `head()` funciona executando
o pedaço abaixo:
```{r}
cabeça(pessoas)
```
```{r}
str(pessoas)
```
```{r}
vislumbre(pessoas)
```
Você também pode usar `colnames()` para obter uma lista dos nomes de
colunas no seu conjunto de dados. Execute o pedaço de código abaixo
para verificar esta função:
```{r}
nomes de colunistas (pessoas)
```
Agora que você tem um data frame, pode trabalhar com ele usando todas
as ferramentas em `R`. Por exemplo, você pode usar `mutate()` se
quiser criar uma nova variável que capture a idade de cada pessoa em
vinte anos. O pedaço de código abaixo cria essa nova variável:
```{r}
mutate(pessoas, idade_em_20 = idade + 20)
```
```{r}
fruta <- c("Limão", "Mirtilo", "Toranja", "Manga", "Morango")
```
Por fim, combine os dois vetores em um data frame. Você pode chamá-lo
de `fruit_ranks`. Edite o pedaço de código abaixo e execute-o para
criar seu data frame.
```{r}
fruit_ranks <- data.frame(fruta, classificação)
```