Removing a data subset

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Removing a data subset

kydaviddoyle
Say I have a dataset that looks like

Location    Year      GW_Elv
MW01        1999       546.63
MW02        1999       474.21
MW03        1999       471.94
MW04        1999        466.80
MW01        2000        545.90
MW02        2000        546.10

The whole dataset is at http://doylesdartden.com/ExampleData.csv
and I use the code below to do the graph but I want to do it without MW01.
How can I remove MW01??

I'm sure I can do it by SubSeting but I can not figure out how to do it.

Thank you
David

--------------------------------------------------------------

library(ggplot2)

MyData <- read.csv("http://doylesdartden.com/ExampleData.csv", header=TRUE,
sep=",")



#Sets whic are detections and nondetects
MyData$Detections <- ifelse(MyData$D_GW_Elv ==1, "Detected", "NonDetect")

#Removes the NAs
MyDataWONA <- MyData[!is.na(MyData$Detections), ]

#does the plot
p <- ggplot(data = MyDataWONA, aes(x=Year, y=GW_Elv , col=Detections)) +
  geom_point(aes(shape=Detections)) +

  ##sets the colors
  scale_colour_manual(values=c("black","red")) + #scale_y_log10() +

  #location of the legend
  theme(legend.position=c("right")) +

  #sets the line color, type and size
  geom_line(colour="black", linetype="dotted", size=0.5) +
  ylab("Elevation Feet Mean Sea Level")

## does the graph using the Location IDs as the different Locations.
p + facet_grid(Location ~ .)

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Take Care

David Doyle
Reply | Threaded
Open this post in threaded view
|

Re: Removing a data subset

Rainer Schuermann
Reading in the data from the file

    x <- read.csv( "ExampleData.csv", header = TRUE, stringsAsFactors = FALSE )

Subsetting  as you want

    x <- x[ x$Location != "MW01", ]

This selects all rows where the value in column 'Location' is not equal to "MW01". The comma after that ensures that all columns are copied into the amended data.frame.

Rgds,
Rainer

On Mittwoch, 29. November 2017 15:07:34 +08 David Doyle wrote:

> Say I have a dataset that looks like
>
> Location    Year      GW_Elv
> MW01        1999       546.63
> MW02        1999       474.21
> MW03        1999       471.94
> MW04        1999        466.80
> MW01        2000        545.90
> MW02        2000        546.10
>
> The whole dataset is at http://doylesdartden.com/ExampleData.csv
> and I use the code below to do the graph but I want to do it without MW01.
> How can I remove MW01??
>
> I'm sure I can do it by SubSeting but I can not figure out how to do it.
>
> Thank you
> David
>
> --------------------------------------------------------------
>
> library(ggplot2)
>
> MyData <- read.csv("http://doylesdartden.com/ExampleData.csv", header=TRUE,
> sep=",")
>
>
>
> #Sets whic are detections and nondetects
> MyData$Detections <- ifelse(MyData$D_GW_Elv ==1, "Detected", "NonDetect")
>
> #Removes the NAs
> MyDataWONA <- MyData[!is.na(MyData$Detections), ]
>
> #does the plot
> p <- ggplot(data = MyDataWONA, aes(x=Year, y=GW_Elv , col=Detections)) +
>   geom_point(aes(shape=Detections)) +
>
>   ##sets the colors
>   scale_colour_manual(values=c("black","red")) + #scale_y_log10() +
>
>   #location of the legend
>   theme(legend.position=c("right")) +
>
>   #sets the line color, type and size
>   geom_line(colour="black", linetype="dotted", size=0.5) +
>   ylab("Elevation Feet Mean Sea Level")
>
> ## does the graph using the Location IDs as the different Locations.
> p + facet_grid(Location ~ .)
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Removing a data subset

Ivan Calandra-5
In reply to this post by kydaviddoyle
Hi David,

You "just" need to learn how to subset your data.frame, see functions
like ?subset and ?"[", as well as a good guide to understand the subtleties!

Some graphic functions also have a built-in argument to subset within
the function (e.g. argument 'subset' in 'plot.formula'), although the
ggplot() function doesn't seem to have it.

In any case, I would recommend you spend some time learning that aspect,
as you will always need it in one situation or another.

HTH,
Ivan

--
Dr. Ivan Calandra
TraCEr, laboratory for Traceology and Controlled Experiments
MONREPOS Archaeological Research Centre and
Museum for Human Behavioural Evolution
Schloss Monrepos
56567 Neuwied, Germany
+49 (0) 2631 9772-243
https://www.researchgate.net/profile/Ivan_Calandra

On 29/11/2017 22:07, David Doyle wrote:

> Say I have a dataset that looks like
>
> Location    Year      GW_Elv
> MW01        1999       546.63
> MW02        1999       474.21
> MW03        1999       471.94
> MW04        1999        466.80
> MW01        2000        545.90
> MW02        2000        546.10
>
> The whole dataset is at http://doylesdartden.com/ExampleData.csv
> and I use the code below to do the graph but I want to do it without MW01.
> How can I remove MW01??
>
> I'm sure I can do it by SubSeting but I can not figure out how to do it.
>
> Thank you
> David
>
> --------------------------------------------------------------
>
> library(ggplot2)
>
> MyData <- read.csv("http://doylesdartden.com/ExampleData.csv", header=TRUE,
> sep=",")
>
>
>
> #Sets whic are detections and nondetects
> MyData$Detections <- ifelse(MyData$D_GW_Elv ==1, "Detected", "NonDetect")
>
> #Removes the NAs
> MyDataWONA <- MyData[!is.na(MyData$Detections), ]
>
> #does the plot
> p <- ggplot(data = MyDataWONA, aes(x=Year, y=GW_Elv , col=Detections)) +
>    geom_point(aes(shape=Detections)) +
>
>    ##sets the colors
>    scale_colour_manual(values=c("black","red")) + #scale_y_log10() +
>
>    #location of the legend
>    theme(legend.position=c("right")) +
>
>    #sets the line color, type and size
>    geom_line(colour="black", linetype="dotted", size=0.5) +
>    ylab("Elevation Feet Mean Sea Level")
>
> ## does the graph using the Location IDs as the different Locations.
> p + facet_grid(Location ~ .)
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Removing a data subset

kydaviddoyle
In reply to this post by Rainer Schuermann
Thank Rainer and Jim!!

The end result was:
#Makes a new data set name "MyData_Minus_MW01" that contains all the data
where the Location Column is not equal to MW01 and the comma after that
ensures that all columns are copied into the amended data.frame.
MyData_Minus_MW01 <- MyData[ MyData$Location != "MW01", ]


The final code is as follows:

#Loads ggplot2
library(ggplot2)

#Loads data from internet and names it MyData
MyData <- read.csv( "http://doylesdartden.com/ExampleData.csv", header =
TRUE, stringsAsFactors = FALSE )

#Makes a new data set name "MyData_Minus_MW01" that contains all the data
where the Location Column is not equal to MW01 and the comma after that
ensures that all columns are copied into the amended data.frame.
MyData_Minus_MW01 <- MyData[ MyData$Location != "MW01", ]

#Sets whic are detections and nondetects
MyData_Minus_MW01$Detections <- ifelse(MyData_Minus_MW01$D_GW_Elv ==1,
"Detected", "NonDetect")

#Removes the NAs
MyData_Minus_MW01WONA <-
MyData_Minus_MW01[!is.na(MyData_Minus_MW01$Detections),
]

#does the plot
p <- ggplot(data = MyData_Minus_MW01WONA, aes(x=Year, y=GW_Elv ,
col=Detections)) +
  geom_point(aes(shape=Detections)) +

  ##sets the colors
  scale_colour_manual(values=c("black","red")) + #scale_y_log10() +

  #location of the legend
  theme(legend.position=c("right")) +

  #sets the line color, type and size
  geom_line(colour="black", linetype="dotted", size=0.5) +
  ylab("Elevation Feet Mean Sea Level")

## does the graph using the Location IDs as the different Locations.
p + facet_grid(Location ~ .)


Thanks again
David

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Take Care

David Doyle