gpt4 book ai didi

R 清理 csv 文件

转载 作者:行者123 更新时间:2023-12-04 10:46:02 26 4
gpt4 key购买 nike

我正在研究天气数据。我正在尝试清理一个 csv 文件,但不断出现奇怪的错误。在 csv 文件中,9999 和 -9999 是空值。我想删除超过一半的值是 9999 或 -9999 的所有列。我读入了 csv 文件。然后初始化一个空矩阵,使其与读取 csv 文件时创建的数据框具有相同的维度。我遍历数据框"file"中的列,检查是否有任何值是 na 或空字符串。我创建了一个向量“元”并从 plyr 库调用计数来聚合数据框中的所有列。然后我遍历行并检查行中是否有任何 na 值,然后我检查 9999 或 -9999 是否在向量元行中。然后我查看 的频率是否大于列中行数的一半。如果是,则将值添加到干净矩阵。我不断收到奇怪的错误,干净矩阵中的值不是我预期的。我将附上两者的屏幕截图。我还将附上 csv 文件和 R 代码。

谢谢

errors values in the clean matrix 1

file <- read.csv(header = TRUE, file = "C:\\Users\\michael.guarino1\\Desktop\\weather\\nov_6_2012\\735144.csv")
clean <- matrix(0, nrow(file), ncol(file))#initialize matrix of same dimensions as file
library(plyr)
count <- 1 #initialize count to 1
for (i in 1:ncol(file)) {
if (!any(is.na(file[,i]))|!any(file[,i]==" ")) {
meta <- count(file, i)
for(row in 1:nrow(meta)){
if(!is.na(meta[row,1])){
if (meta[row,1]== -9999 | meta[row,1]== 9999) {
print("condition 1 found")

if (meta$freq < (nrow(file) / 2)) {
print("condition 2 found")
clean[, count] <- file[, i]

count <- count + 1
}
}
}

#else{
# clean[, count] <- file[, i]
#count <- count + 1
# }
}

}
else{
next
}
}
print("meta")
print(meta)
print("clean")
print(clean)


STATION,STATION_NAME,ELEVATION,LATITUDE,LONGITUDE,DATE,MDPR,Measurement Flag,Quality Flag,Source Flag,Time of Observation,MDSF,Measurement Flag,Quality Flag,Source Flag,Time of Observation,DAPR,Measurement Flag,Quality Flag,Source Flag,Time of Observation,DASF,Measurement Flag,Quality Flag,Source Flag,Time of Observation,PRCP,Measurement Flag,Quality Flag,Source Flag,Time of Observation,SNWD,Measurement Flag,Quality Flag,Source Flag,Time of Observation,SNOW,Measurement Flag,Quality Flag,Source Flag,Time of Observation,TMAX,Measurement Flag,Quality Flag,Source Flag,Time of Observation,TMIN,Measurement Flag,Quality Flag,Source Flag,Time of Observation,TOBS,Measurement Flag,Quality Flag,Source Flag,Time of Observation,PGTM,Measurement Flag,Quality Flag,Source Flag,Time of Observation,WT09,Measurement Flag,Quality Flag,Source Flag,Time of Observation,WT07,Measurement Flag,Quality Flag,Source Flag,Time of Observation,WT01,Measurement Flag,Quality Flag,Source Flag,Time of Observation,WT06,Measurement Flag,Quality Flag,Source Flag,Time of Observation,WT05,Measurement Flag,Quality Flag,Source Flag,Time of Observation,WT02,Measurement Flag,Quality Flag,Source Flag,Time of Observation,WT11,Measurement Flag,Quality Flag,Source Flag,Time of Observation,WT04,Measurement Flag,Quality Flag,Source Flag,Time of Observation,WT16,Measurement Flag,Quality Flag,Source Flag,Time of Observation,WT08,Measurement Flag,Quality Flag,Source Flag,Time of Observation,WT18,Measurement Flag,Quality Flag,Source Flag,Time of Observation,WT03,Measurement Flag,Quality Flag,Source Flag,Time of Observation
GHCND:USR0000IPOB,POTTER BUTTE IDAHO ID US,1502.7,43.2261,-113.5744,20081104,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,40,H, ,U,9999,30,H, ,U,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999
GHCND:USW00004842,WOOSTER WAYNE CO AIRPORT OH US,346.6,40.87306,-81.88667,20081104,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,0.00, , ,W,9999,-9999, , , ,9999,-9999, , , ,9999,71, , ,W,9999,40, , ,W,9999,-9999, , , ,9999,1605, , ,W,9999,-9999, , , ,9999,-9999, , , ,9999,1, , ,W,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,1, , ,W,9999,-9999, , , ,9999,-9999, , , ,9999
GHCND:USW00003954,OKLAHOMA CITY WILEY POST AIRPORT OK US,395.3,35.53417,-97.64694,20081104,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,0.00, , ,W,9999,-9999, , , ,9999,-9999, , , ,9999,77, , ,W,9999,57, , ,W,9999,-9999, , , ,9999,1531, , ,W,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999
GHCND:USC00080737,BIG CYPRESS FL US,4.6,26.32833,-80.99583,20081104,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,0.00, , ,0,0800,0.0,P, ,0,9999,0.0,P, ,0,9999,82, , ,0,0800,65, , ,0,0800,65, , ,0,0800,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999
GHCND:USR0000OBRI,BRER RABBIT OREGON OR US,1798.3,44.3231,-119.7669,20081104,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,31,H, ,U,9999,22,H, ,U,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999
GHCND:USC00202381,EAST JORDAN MI US,178.3,45.1519,-85.1322,20081104,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,0.00, , ,0,1500,0.0, , ,0,1500,0.0, , ,0,9999,74, , ,0,1500,43, , ,0,1500,74, , ,0,1500,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,1, , ,0,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999
GHCND:USR0000CKON,KONOCTI CALIFORNIA CA US,659.3,38.9119,-122.7064,20081104,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,49,H, ,U,9999,40,H, ,U,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999
GHCND:USR0000MBLA,BLACKWATER MARYLAND MD US,426.7,38.4167,-76,20081104,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,61,H, ,U,9999,54,H, ,U,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999
GHCND:USR0000WEST,ESTERBROOK WYOMING WY US,1990.3,42.4153,-105.3611,20081104,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,56,H, ,U,9999,32,H, ,U,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999
GHCND:US1GAGW0006,BUFORD 4.6 ESE GA US,352.7,34.0799,-83.9313,20081104,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,0.00, , ,N,9999,-9999, , , ,9999,0.0, , ,N,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999
GHCND:USC00111290,CARLYLE RESERVOIR IL US,152.7,38.63083,-89.36583,20081104,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,0.00, , ,0,0800,0.0,P, ,0,9999,0.0,P, ,0,9999,76, , ,0,0800,46, , ,0,0800,49, , ,0,0800,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999
GHCND:USC00116661,PAW PAW 2 S IL US,289.6,41.71222,-88.99889,20081104,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,0.00, , ,0,0700,0.0, , ,0,9999,0.0, , ,0,9999,72, , ,0,0700,47, , ,0,0700,48, , ,0,0700,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999
GHCND:USR0000MSUN,SUNKHAZE MEADOWS MAINE ME US,34.7,44.9031,-68.64,20081104,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,63,H, ,U,9999,35,H, ,U,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999
GHCND:USR0000MAVA,AVA MISSOURI MO US,399.6,36.9431,-92.65,20081104,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,73,H, ,U,9999,44,H, ,U,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999
GHCND:USR0000JEBF,EB FORSYTHE NEW JERSEY NJ US,396.2,39.5,-74.5,20081104,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,60,H, ,U,9999,51,H, ,U,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999
GHCND:USR0000ADVK,DEVILS KNOB ARKANSAS AR US,640.1,35.6111,-93.3333,20081104,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,65,H, ,U,9999,54,H, ,U,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999
GHCND:USC00228556,SUMRALL MS US,88.4,31.42222,-89.53861,20081104,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,0.00,P, ,0,0700,0.0,P, ,0,9999,0.0,P, ,0,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999
GHCND:USR0000TDAY,DAYTON TEXAS TX US,30.5,30.105,-94.9314,20081104,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,79,H, ,U,9999,48,H, ,U,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999
GHCND:USC00340292,ARDMORE OK US,268.2,34.17139,-97.12944,20081104,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,0.00, , ,0,0800,0.0,P, ,0,9999,0.0,P, ,0,9999,78, , ,0,0800,60, , ,0,0800,67, , ,0,0800,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999
GHCND:US1PALN0003,ADAMSTOWN 2.5 SSE PA US,198.1,40.2055,-76.0509,20081104,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,0.00,T, ,N,9999,-9999, , , ,9999,0.0, , ,N,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999
GHCND:USC00408522,SPARTA WASTEWATER PLANT TN US,310.9,35.9566,-85.4813,20081104,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,0.00, , ,0,0700,0.0,P, ,0,9999,0.0,P, ,0,9999,75, , ,0,0700,36, , ,0,0700,39, , ,0,0700,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999
GHCND:USR0000WGOL,GOLD MOUNTAIN WASHINGTON WA US,1428.3,48.1806,-118.4636,20081104,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,33,H, ,U,9999,27,H, ,U,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999
GHCND:US1FLAL0007,GAINESVILLE 8.1 SW FL US,36.9,29.5908,-82.4314,20081104,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,0.00, , ,N,9999,-9999, , , ,9999,0.0, , ,N,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999
GHCND:USC00148341,VALLEY FALLS KS US,299.3,39.3033,-95.4861,20081104,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,0.00, , ,0,0700,0.0,P, ,0,0700,0.0,P, ,0,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999
GHCND:USR0000IHAR,HARDIN RIDGE INDIANA IN US,228.6,39,-86.4228,20081104,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,76,H, ,U,9999,43,H, ,U,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999
GHCND:USW00014742,BURLINGTON INTERNATIONAL AIRPORT VT US,100.6,44.46806,-73.15028,20081104,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,0.00, , ,0,2400,0.0, , ,0,9999,0.0, , ,0,9999,61, , ,0,2400,46, , ,0,2400,-9999, , , ,9999,941, , ,W,9999,-9999, , , ,9999,-9999, , , ,9999,1, , ,W,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,1, , ,W,9999,-9999, , , ,9999,-9999, , , ,9999
GHCND:USR0000SWIT,WITHERBEE SOUTH CAROLINA SC US,18,33.1597,-79.8306,20081104,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,62,H, ,U,9999,59,H, ,U,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999
GHCND:USC00367782,SALINA 3 W PA US,338,40.5101,-79.5459,20081104,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,0.00, , ,0,0700,0.0,P, ,0,0700,0.0,P, ,0,9999,69, , ,0,0700,43, , ,0,0700,44, , ,0,0700,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999
GHCND:USR0000CANZ,ANZA CALIFORNIA CA US,1194.8,33.555,-116.673,20081104,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,53,H, ,U,9999,42,H, ,U,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999
GHCND:US1KSEL0043,HAYS 5.4 SSW KS US,630.6,38.8129,-99.3735,20081104,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,0.00, , ,N,9999,-9999, , , ,9999,0.0, , ,N,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999
GHCND:USR0000ATAL,TALLADEGA ALABAMA AL US,182.9,33.4411,-86.0811,20081104,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,76,H, ,U,9999,44,H, ,U,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999
GHCND:USW00094985,MARSHFIELD MUNICIPAL AIRPORT WI US,382.5,44.63806,-90.1875,20081104,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,0.00, , ,W,9999,-9999, , , ,9999,-9999, , , ,9999,73, , ,W,9999,53, , ,W,9999,-9999, , , ,9999,1436, , ,W,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999
GHCND:USR0000ISLA,SLATE CREEK IDAHO ID US,477.9,45.6333,-116.2833,20081104,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,51,H, ,U,9999,41,H, ,U,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999
GHCND:USR0000MDRY,DRY BLOOD CREEK MONTANA MT US,914.4,47.2442,-108.3575,20081104,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,53,H, ,U,9999,38,H, ,U,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999
GHCND:USC00134142,IOWA FALLS IA US,321.6,42.5188,-93.2536,20081104,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,0.00,P, ,0,0700,0.0,P, ,0,9999,0.0,P, ,0,9999,75, , ,0,0700,52, , ,0,0700,55, , ,0,0700,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999
GHCND:USC00161565,CARVILLE 2 SW LA US,7.6,30.19806,-91.12556,20081104,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,0.00, , ,0,2400,0.0,P, ,0,9999,0.0,P, ,0,9999,77, , ,0,2400,51, , ,0,2400,58, , ,0,2400,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999
GHCND:USR0000NSTO,STONYKILL NEW YORK NY US,61,41.5,-73.9,20081104,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,63,H, ,U,9999,40,H, ,U,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999
GHCND:US1ALMD0040,HAMPTON COVE 0.5 NNW AL US,187.1,34.667,-86.485,20081104,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,0.00, , ,N,9999,-9999, , , ,9999,0.0, , ,N,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999
GHCND:USC00421446,CITY CRK WATER PLANT UT US,1624.6,40.8397,-111.8313,20081104,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,0.62, , ,0,1600,1.0, , ,0,1600,1.3, , ,0,9999,52, , ,0,1600,32, , ,0,1600,40, , ,0,1600,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999,-9999, , , ,9999

最佳答案

假设您的数据如下所示:

df <- data.frame(v1 = c(1,1,2,1,2,1),
v2 = c(9999, 1, 2, 1, 2, -9999),
v3 = c(9999, 1, -9999, 9999, 2, -9999))

然后,您可以使用 colSums 删除超过一半值为 9999 或 -9999 的列:

df[ , colSums(abs(df) == 9999)/nrow(df) < 0.5]

或使用colMeans:

df[ , colMeans(abs(df) == 9999) < 0.5]

两者都会导致删除 v3 列:

  v1    v2
1 1 9999
2 1 1
3 2 2
4 1 1
5 2 2
6 1 -9999

关于R 清理 csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37280024/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com