[R] Removing a data subset

Rainer Schuermann Rainer.Schuermann at gmx.net
Wed Nov 29 23:31:13 CET 2017


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]]
> 
> ______________________________________________
> R-help at r-project.org 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.
>



More information about the R-help mailing list