Home > Enterprise >  subseting a dataframe in R
subseting a dataframe in R

Time:10-17

I have a dataframe and I want to Create a subset,< Frame>, of just the species variable and display the first five records. with R how can I subset? there are 10 rows and 7 columns.one column is Species

netID- fishID - species- tl - wtag - scale

CodePudding user response:

By select. head( select(dataframe, speceis) )

CodePudding user response:

Assuming your dataframe is called df you can subset with dplyr

library(dplyr)
df <- iris[1:10,]

  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1           5.1         3.5          1.4         0.2  setosa
2           4.9         3.0          1.4         0.2  setosa
3           4.7         3.2          1.3         0.2  setosa
4           4.6         3.1          1.5         0.2  setosa
5           5.0         3.6          1.4         0.2  setosa
6           5.4         3.9          1.7         0.4  setosa
7           4.6         3.4          1.4         0.3  setosa
8           5.0         3.4          1.5         0.2  setosa
9           4.4         2.9          1.4         0.2  setosa
10          4.9         3.1          1.5         0.1  setosa

newdf<-df %>% select(Species) %>%slice(1:5)

Here you are selecting species from your data frame and then using slice you can select the range of rows you need. The Output of newdf is

  Species
1   setosa
2   setosa
3   setosa
4   setosa
5   setosa
  • Related