Evaluate column of matrix

纵然是瞬间 提交于 2020-02-08 09:36:11

问题


I am trying to evaluate the 8th column of a matrix

sep <- read.csv("California_SEP_assets_csv.csv")
Sub1 <- sep[grep("SEP.11", names(sep))]
sep$newCol <- 100*rowSums(Sub1)/rowSums(sep[4:7])
library(sp)
coords <- cbind(Longitude = as.numeric(as.character(sep$Longitude)),Latitude=as.numeric(as.character(sep$Latitude)))

if (sep[8] > 50){
  sep.pts <- SpatialPointsDataFrame(coords,sep[,-(2:3)],proj4string = CRS("+init=epsg:4326"))
} else  {
  sep2.pts <- SpatialPointsDataFrame(coords,sep[,-(2:3)],proj4string = CRS("+init=epsg:4326"))
}

I get warning

the condition has length > 1 and only the first element will be used

What is the syntax to evaluate each each entry under a single column?

Here is the matrix

dput(head(sep))
structure(list(Site = structure(1:6, .Label = c("31R001", "31R002", 
"31R003", "31R004", "31R005", "31R006", "31R007", "31R008", "31R011", 
"31R013", "31R014", "31R016", "31R018", "31R019", "31R020", "31R021", 
"31R022", "31R023", "31R024", "31R025", "31R026", "31R027", "31R029", 
"31R030", "31R031", "31R032", "31R034", "31R035", "31R036", "31R038", 
"31R039", "31R040", "31R041", "31R042", "31R043", "31R044", "31R045", 
"31R046", "31R048", "31R049", "31R050", "31R051", "31R052", "31R053", 
"31R054", "31R055", "31R056", "31R057", "31R058", "31R059", "31R060", 
"31R061", "31R069", "31R071", "31R072", "31R075", "31R435", "31R440", 
"31R445", "31R450", "31R455", "31R460", "31R470", "31R600", "31R722", 
"31R801", "31R825", "31R826", "31R829", "31R840", "31R843", "31R861", 
"31R880"), class = "factor"), Latitude = c(33.808874, 33.877256, 
33.820825, 33.852373, 33.829697, 33.810274), Longitude = c(-117.844048, 
-117.700135, -117.811845, -117.795516, -117.787532, -117.830429
), Windows.SEP.11 = c(63L, 174L, 11L, 85L, 163L, 71L), Mac.SEP.11 = c(0L, 
1L, 4L, 0L, 0L, 50L), Windows.SEP.12 = c(124L, 185L, 9L, 75L, 
23L, 5L), Mac.SEP.12 = c(0L, 1L, 32L, 1L, 0L, 50L), newCol = c(33.6898395721925, 
48.4764542936288, 26.7857142857143, 52.7950310559006, 87.6344086021505, 
68.75)), .Names = c("Site", "Latitude", "Longitude", "Windows.SEP.11", 
"Mac.SEP.11", "Windows.SEP.12", "Mac.SEP.12", "newCol"), row.names = c(NA, 
6L), class = "data.frame")

Eventually, I'd like to color-code plots on Google map where the ones that are >= 50 are red, and < 50% are green

More Info

My original code gave only red points, regardless

library(ggmap)
setwd("d:/GIS/31R")
sep <- read.csv("California_SEP_assets_csv.csv")
library(sp)
coords <- cbind(Longitude = as.numeric(as.character(sep$Longitude)),Latitude=as.numeric(as.character(sep$Latitude)))
sep.pts <- SpatialPointsDataFrame(coords,sep[,-(2:3)],proj4string = CRS("+init=epsg:4326"))
plot(sep.pts, pch=".",col="darkred")
map <- qmap('Yorba Linda', zoom = 11, maptype = 'hybrid')
map + geom_point(data=sep, aes(x = Longitude, y = Latitude), color="red", size = 5, alpha = 0.5)

Which gave

Another Update

I changed code to remove ,-(2:3) from SpatialPointsDataFrame. I really don't understand the significance of removing the longitude and latitude columns, but at least it doesn't give error.

However, I am researching how to plot SpatialPointsDataFrame on a map.

Here is the code so far

library(ggmap)
setwd("d:/GIS/31R")
sep <- read.csv("California_SEP_assets_csv.csv")
Sub1 <- sep[grep("SEP.11", names(sep))]
sep$newCol <- 100*rowSums(Sub1)/rowSums(sep[4:7])
library(sp)
coords <- cbind(Longitude = as.numeric(as.character(sep$Longitude)),Latitude=as.numeric(as.character(sep$Latitude)))


sep.pts <- SpatialPointsDataFrame(coords,sep[sep.pts$newCol >= 50], proj4string = CRS("+init=epsg:4326"))
sep2.pts <- SpatialPointsDataFrame(coords,sep[sep.pts$newCol < 50], proj4string = CRS("+init=epsg:4326"))

map <- qmap('Yorba Linda', zoom = 11, maptype = 'hybrid')
map + geom_point(data=sep.pts, aes(x = Longitude, y = Latitude), color="red", size = 5, alpha = 0.5) + geom_point(data=sep2.pts, aes(x = Longitude, y = Latitude), color="green", size = 5, alpha = 0.5)

and it gives error

Error: ggplot2 doesn't know how to deal with data of class SpatialPointsDataFrame


回答1:


ggplot2 is not my specialty but the structure of implied conditionals goes like this:

If I had a data frame of df <- data.frame(x=1:3, y=4:6).

df
  x y
1 1 4
2 2 5
3 3 6

I could create two data frames for values of x greater than one and those that are not. I want it to look like this:

df1
  x y
1 1 4

df2
  x y
2 2 5
3 3 6

I could get there with:

split(df, df$x > 1)
$`FALSE`
  x y
1 1 4

$`TRUE`
  x y
2 2 5
3 3 6

Update -

We can assign the function to a variable.

lst <- split(df, df$x > 1)
df1 <- lst[[1]]
df2 <- lst[[2]]

In your case,

lst <- split(sep, sep[,8] >= 50)
under50 <- lst[[1]]
over50 <- lst[[2]] 



回答2:


sep.pts <- SpatialPointsDataFrame(coords,sep[sep.pts$newCol >= 50, -(2:3)],proj4string = CRS("+init=epsg:4326"))
sep2.pts <- SpatialPointsDataFrame(coords,sep[sep.pts$newCol < 50,-(2:3)],proj4string = CRS("+init=epsg:4326"))

I'm assuming per your last line you wanted >= and < 50 rather than > and <=.

You need to pass your logical vector as the first argument to the subset operator as shown here.



来源:https://stackoverflow.com/questions/30988477/evaluate-column-of-matrix

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!