本文介绍了评估矩阵的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在评估矩阵的第8列 sep< - read.csv(California_SEP_assets_csv.csv ) Sub1 sep $ newCol 库(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 = $ p $ b $ p 条件有长度> 1,并且只有第一个元素将被使用 什么是语法来评估每个条目列? 以下是矩阵 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, 31R023,31R024,31R025,31R026,31R027,31R029,$ $ $ $31R030,31R031,31R032,31R034 ,31R036,31R038,31R038,31R038,31R040,31R040,31R039,31R040,31R041, 31R055,31R051,31R050,31R051,31R052,31R053,,31R054,31R055,31R056,31R057,31R058 ,31R060,31R060,31R060,31R070,31R070,31R070,31R070,31R040,31R450, 31R455,31R460,31R470,31R600,31R722,31R801, (33.808874,33.877256,$ b $,31R825,31R826,31R829,31R840,31R843, b 33.820825,33.852373,33.829697,33.810274),经度= 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.8857142857143,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) 最后,我想在Google地图上标出> = 50 是红色的,< 50%是绿色的 更多信息 ,无论是 library(ggmap) setwd(d:/ GIS / 31R) sepd < - read.csv(California_SEP_assets_csv.csv)库(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 map + geom_point(data = sep,aes(x = Longitude,y = Latitude),color =red,size = 5,alpha = 0.5) 哪给了 另一个更新 我更改了代码以删除, - (2:3)来自SpatialPointsDataFrame。我真的不明白删除经纬度列的意义,但至少它不会给出错误。 然而,我正在研究如何绘制SpatialPointsDataFrame 这是目前的代码 图书馆(ggmap) setwd(d:/ GIS / 31R) sep< - read.csv(California_SEP_assets_csv.csv) Sub1< - sep [grep(SEP (s)($ s $($) $ b $ ; - 经度= as.numeric(as.character(sep $经度)),纬度= as.numeric(as.character(sep $纬度))) sep。 pts< - SpatialPointsDataFrame(coords,sep [sep.pts $ newCol> = 50],proj4string = CRS(+ init = epsg:4326)) sep2.pts< - SpatialPointsDataFrame(coords,sep [sep.pts $ newCol map 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 = 5,alpha = 0.5) 并且它给出错误 错误:ggplot2不知道如何处理类SpatialPointsDataFrame的数据 解决方案 ggplot2不是我的专业,但隐含条件的结构如下所示: 如果我有一个 df 。 df xy 1 1 4 2 2 5 3 3 6 我可以为x大于1的值创建两个数据框,而不是。我希望它看起来像这样: df1 xy 1 1 4 df2 xy 2 2 5 3 3 6 我可以用: split(df,df $ x> 1) $`FALSE` xy 1 1 4 $`TRUE` xy 2 2 5 3 3 6 更新 - 我们可以将函数分配给一个变量。 b $ b lst 1) df1 df2 在您的情况下, lst = 50) under50 over 50 I am trying to evaluate the 8th column of a matrixsep <- 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 usedWhat is the syntax to evaluate each each entry under a single column?Here is the matrixdput(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 greenMore InfoMy 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 gaveAnother UpdateI 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 farlibrary(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 解决方案 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 y1 1 42 2 53 3 6I 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 y1 1 4df2 x y2 2 53 3 6I could get there with:split(df, df$x > 1)$`FALSE` x y1 1 4$`TRUE` x y2 2 53 3 6Update - 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]] 这篇关于评估矩阵的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-02 04:08