Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从R中的csv文件中提取行_R - Fatal编程技术网

从R中的csv文件中提取行

从R中的csv文件中提取行,r,R,我想从csv中提取lat/long数据+文件名 我已经做了以下工作: #libraries----- library(readr) library("dplyr") library("tidyverse") # set wd-----EXAMPLE setwd("F:/mydata/myfiles/allcsv") # have R read files as list ----- list <- list.files("F:/mydata/myfiles/allcsv", patt

我想从csv中提取lat/long数据+文件名

我已经做了以下工作:

#libraries-----
library(readr)
library("dplyr")
library("tidyverse")


# set wd-----EXAMPLE
setwd("F:/mydata/myfiles/allcsv")

# have R read files as list -----
list <- list.files("F:/mydata/myfiles/allcsv", pattern=NULL, all.files=FALSE,
                  full.names=FALSE)
list
]

#lapply function
row.names<- c("Date=0", "Time=3", "Type=2", "Model=1", "Coordinates=nextrow", "Latitude = 38.3356", "Longitude = 51.3323")
AllData <- lapply(list, read.table, 
                  skip=5, header=FALSE, sep=";", row.names=row.names, col.names=NULL)

PulledRows <- 
  lapply(AllData, function(DF) 
    DF[fileone$Latitude==38.3356, fileone$Longitude==51.3323]
  )

# maybe i need to specify a for loop?
#库-----
图书馆(readr)
图书馆(“dplyr”)
图书馆(“tidyverse”)
#设置wd------示例
setwd(“F:/mydata/myfiles/allcsv”)
#让R以列表形式读取文件-----

这应该对你有用。如果.csv文件不在您的工作目录中,您可能必须更改路径位置。以及保存最终结果的位置

results <- data.frame(Latitude=NA,Longitude=NA,FileName=NA) #create empty dataframe
for(i in 1:length(list)){ # loop through each file obtained from list (called above)
  dat <- read_csv(list[i],col_names = FALSE) # read in the ith dataset
  df <- data.frame(dat[6,1],dat[7,1],list[i]) # create new dataframe with values from dat
  df[,1] <- as.numeric(str_remove(df[,1],'Latitude=')) # remove text and make numeric
  df[,2] <- as.numeric(str_remove(df[,2],'Longitude='))
  names(df) <- names(results) # having the same column names allows next line
  results <- rbind(results,df) # 'stacks' the results dataframe and df dataframe
}
results <- na.omit(results) # remove missing values (first row)
write_csv(results,'desired/path')

结果所有csv文件中的数据是否一致?i、 纬度总是在第六格吗?或者至少总是说纬度=…?是的。它们总是出现在同一个单元格中,表示“纬度=#$#%$”,表示经度。但是,值会发生变化。我在回答中添加了一些注释。这真的不是一个很好的地方,可以一行一行地教你,但是你走的是正确的道路。我会花一些时间剖析它,并试图理解每一行的作用。这里的主题可以是数据帧的子集、循环等。