小编asa*_*o23的帖子

删除重复的列dplyr

今天早上在使用数据框进行一些分析时,由于存在重复的列名,我收到了错误.我试图找到一个专门使用dplyr的解决方案,但我找不到任何有效的方法.这是一个说明问题的例子.具有重复列名称的数据框.

x <- data.frame(matrix(c(1, 2, 3),
                c(2,2,1),nrow=2,ncol=3))
colnames(x) <- c("a", "a", "b")
Run Code Online (Sandbox Code Playgroud)

当我尝试使用select命令删除第一列时,我收到一个错误

x %>%
  select(-1)%>%filter(b>1)

Error: found duplicated column name: a
Run Code Online (Sandbox Code Playgroud)

我可以使用传统的索引和使用dplyr按值过滤来轻松删除列

x<-x[,-1]%>%filter(b>1)
Run Code Online (Sandbox Code Playgroud)

这产生了所需的输出

> x
  a b
1 2 3
2 2 3
Run Code Online (Sandbox Code Playgroud)

关于如何仅使用dplyr语法执行此操作的任何想法?

r dplyr

9
推荐指数
1
解决办法
2158
查看次数

如何在重新采用pandas时获取字符串变量的模式

我正在尝试将带有时间戳索引的pandas数据帧重新采样为每小时一次.我有兴趣获得具有字符串值的列的最常用值.然而,时间序列重新采样的内置函数不包括模式作为重新采样的默认方法之一(因为它"意味着"和"计数").
我试图定义自己的函数并传递该函数但不起作用.我也尝试过使用该np.bincount功能,但由于我正在处理字符串,所以它不起作用.

以下是我的数据的外观:

                   station_arrived action     lat1     lon1
date_removed
2012-01-01 13:12:00     56             A     19.4171 -99.16561   
2012-01-01 13:12:00     56             A     19.4271 -99.16361 
2012-01-01 15:41:00     56             A     19.4171 -99.16561 
2012-01-02 08:41:00     56             C     19.4271 -99.16561 
2012-01-02 11:36:00     56             C     19.2171 -99.16561
Run Code Online (Sandbox Code Playgroud)

到目前为止这是我的代码:

def mode1(algo):
    common=[ite for ite, it in Counter(algo).most_common(1)]
    # Returns all unique items and their counts
    return common

hourlycount2 = travels2012.resample('H', how={'station_arrived': 'count',
                                              'action': mode(travels2012['action']),
                                              'lat1':'count', 'lon1':'count'})

hourlycount2.head()
Run Code Online (Sandbox Code Playgroud)

我看到以下错误:

Traceback (most recent call last):
  File "<stdin>", line 3, in …
Run Code Online (Sandbox Code Playgroud)

python time-series pandas

5
推荐指数
1
解决办法
2062
查看次数

从R中的字符串中提取数字模式

我对正则表达式比较陌生,而且我正陷入死胡同.我有一个数据框,其列如下所示:

year1
GMM14_2000_NGVA
GMM14_2001_NGVA
GMM14_2002_NGVA
...
GMM14_2014_NGVA
Run Code Online (Sandbox Code Playgroud)

我试图在字符串中间提取年份(2000,2001等).到目前为止,这是我的代码

gsub("[^0-9]","",year1))
Run Code Online (Sandbox Code Playgroud)

返回数字,但它也返回14作为字符串的一部分:

142000
142001
Run Code Online (Sandbox Code Playgroud)

有关如何从模式中排除14或如何更有效地提取年份信息的任何想法?

谢谢

regex r gsub

4
推荐指数
2
解决办法
7202
查看次数

st_intersection 到空间多边形数据框不起作用

我正在尝试获取两个 shapefile(落在某些大都市区边界内的人口普查区)的交集。我能够成功获得相交特征,但是当我尝试将 sf_intersect 的输出转换为 SpatialPolygonsDataframe 时,出现错误:

“as_Spatial(from) 中的错误:不支持从特征类型 sfc_GEOMETRY 到 sp 的转换”

这是我的代码:

library(sf)
library(dplyr)
library(tigris)
library(sp)

#download shapefiles corresponding to metro areas 
metro_shapefiles<-core_based_statistical_areas(cb = FALSE, year = 2016)
#convert to sf and filter
metro_shapefiles<-st_as_sf(metro_shapefiles)%>%filter(GEOID==31080 )
#Data for California
census_tracts_california<-tracts(state="CA",year=2016)
census_tracts_california<-st_as_sf(census_tracts_california)

#INTERSECT AND CONVERT BACK TO SP
census_tracts_intersected1<-st_intersection(census_tracts_california,
                                            metro_shapefiles)

#back to spatial
census_tracts_intersected1<-as(census_tracts_intersected1,"Spatial")
Run Code Online (Sandbox Code Playgroud)

r spatial tigris r-sf

2
推荐指数
1
解决办法
3453
查看次数

标签 统计

r ×3

dplyr ×1

gsub ×1

pandas ×1

python ×1

r-sf ×1

regex ×1

spatial ×1

tigris ×1

time-series ×1