如何在数据框中过滤?

时间:2018-04-22 23:51:13

标签: r dataframe filter dplyr

location    state     data
WA          WA        3
WA          Seattle

我想让它成为唯一状态。过滤后,我想将其存储在新的数据框中,例如

location    state     data
WA          WA        3

我正在尝试用dplyr解决这个问题,我试过

library(dplyr)

state_only <- filter(data, location == state)

但显然它不起作用。有没有更好的方法?谢谢!

2 个答案:

答案 0 :(得分:0)

您可以像这样使用subset()

location <- c("WA", "WA", "WA")
state <- c("WA", "Seattle", "WA")
data <- c(3, 0, 2)

data <- data.frame(location = location, state = state, data = data)
data

subset(data, location == "WA" & state == "WA")

我得到了这个输出:

  location   state data 
1       WA      WA    3  
2       WA Seattle    0 
3       WA      WA    2 

  location state data 
1       WA    WA    3 
3       WA    WA    2 

答案 1 :(得分:0)

或者,您与import numpy as np import csv import matplotlib.pyplot as plt import matplotlib.mlab as mlab data = np.empty([1203]) z = ['NewFile1.csv', 'NewFile2.csv', 'NewFile3.csv', 'NewFile4.csv', 'NewFile5.csv'] ranges = [(-0.85,-0.53), (-0.85,-0.79), (-0.8,-0.7), (-0.83,-0.71), (-0.74,-0.65)] q = 0 for x in z: print('Accessing file', x) with open(x) as csvfile: offset = -2.0E-3 reader = csv.DictReader(csvfile) i = 0 for row in reader: data[i] = row['CH1'] data[i] = (data[i])/pkpk i = i + 1 plt.figure() n, bins, patches = plt.hist(data,bins='auto',normed=1,facecolor='blue',range=ranges[q]) avg = np.mean(bins) sd = np.sqrt(np.var(bins)) plt.plot(bins,mlab.normpdf(bins,avg,sd)) plt.show() q = q + 1 关系密切,请尝试:

dplyr

enter image description here