0 votes
1 view
in R Programming by (5k points)

I have a data frame, str(data) to show more about my data frame the result is the following:

> str(data)

'data.frame':   153 obs. of  6 variables:

$ Ozone  : int  41 36 12 18 NA 28 23 19 8 NA ...

$ Solar.R: int  190 118 149 313 NA NA 299 99 19 194 ...

$ Wind   : num  7.4 8 12.6 11.5 14.3 14.9 8.6 13.8 20.1 8.6 ...

$ Temp   : int  67 72 74 62 56 66 65 59 61 69 ...

$ Month  : int  5 5 5 5 5 5 5 5 5 5 ...

$ Day    : int  1 2 3 4 5 6 7 8 9 10 ...

However, for example, when I want to subset the amounts of Ozone above 14 I use the following code which gives me an error:

>  data[data$Ozone > 14 ]

Error in [.data.frame(data, data$Ozone > 14) : undefined columns selected

1 Answer

0 votes
by (23.2k points)

To select rows where a condition is TRUE, you need to include a comma to specify the rows while indexing.i.e,

data[data$Ozone > 14, ]

For example:

data(iris)

iris[iris$Sepal.Length> 7,]

   Sepal.Length Sepal.Width Petal.Length Petal.Width   Species

103          7.1         3.0          5.9         2.1 virginica

106          7.6         3.0          6.6         2.1 virginica

108          7.3         2.9          6.3         1.8 virginica

110          7.2         3.6          6.1         2.5 virginica

118          7.7         3.8          6.7         2.2 virginica

119          7.7         2.6          6.9         2.3 virginica

...