lapply

How to replace outliers with NA having a particular range of values in R?

六月ゝ 毕业季﹏ 提交于 2020-01-24 21:51:06
问题 I have climate data and I'm trying to replace outliers with NA . I'm not using boxplot(x)$out is because I have a range of values to be considered to compute the outlier. temp_range <- c(-15, 45) wind_range <- c(0, 15) humidity_range <- c(0, 100) My dataframe looks like this df with outliers (I highlighted values that should be replaced with NA according to ranges.) So temp1 and temp2 outliers must be replaced to NA according to temp_range , wind 's outliers should be replaced to NA according

How to call top level from lapply loop (skip/pass)

半世苍凉 提交于 2020-01-23 17:53:26
问题 I want to create a lapply loop that if attends a certain requirement, it will stop... as an example: score <- 0 lapply(1:100, function(z){ score <<- score + 1 if(score >=10){ break } }) However, there is no stop argument as break/pass in a lapply loop. I know this example sounds stupid. However, the original code has too many dependencies to be easily understood... My original loop removes an item from a vector an object every time, however, if there is nothing else to be removed from it can

Nested lapply() in a list?

谁说胖子不能爱 提交于 2020-01-23 07:52:59
问题 I have a list l , which has the following features: It has 3 elements Each element is a numeric vector of length 5 Each vector contains numbers from 1 to 5 l = list(a = c(2, 3, 1, 5, 1), b = c(4, 3, 3, 5, 2), c = c(5, 1, 3, 2, 4)) I want to do two things: First I want to know how many times each number occurs in the entire list and I want each result in a vector (or any form that can allow me to perform computations with the results later): Code 1: > a <- table(sapply(l, "[")) > x <- as.data

changing the columns' values in a list of dataframes in R

雨燕双飞 提交于 2020-01-17 08:13:08
问题 I have a list of dataframe. in each dataframe, I changed the eighth , ninth, and the tenth columns in each dataframe. I defined a vector which represent the the location of the values in the columns that I want to change. aa = seq(1, 168 , 24) bb = rep(T, 168) bb[aa] = FALSE cc= (which(bb)) # vector of locations func.8 = function(x) { x[cc,8] = NA return(x) } func.9 = function(x) { x[cc,9] = NA return(x) } func.10 = function(x) { x[cc,10] = NA return(x) } my.list= lapply( my.list, func.8) my

Recursively extracting values from within a text file and looping over more of them plus rearranging rows and columns

久未见 提交于 2020-01-16 18:15:49
问题 I want to extract values from several hundred txt files based on a regex pattern, rearrange them and write them into a data frame. The beginning of the file starts like this: http://pastebin.com/embed_js.php?i=vdbXfDhC and ends like this: http://pastebin.com/embed_js.php?i=hse7SDJd I had a similar question earlier (Rearranging the structure of many txt files and then merging them in one data frame) where rawr provided me with this code: (lf <- list.files('~/desktop', pattern = '^image\\d+.txt

Creating R loop to read in shapefiles from a directory and perform zonal statistics on each

自古美人都是妖i 提交于 2020-01-16 09:42:03
问题 I have 120 county shapefiles in a directory "Counties". I want to use R to read in each shapefile and, for each shapefile, perform zonal statistics (mean) using a single raster layer "NOAA_normal_crop." I was able to create a script that reads in all of the shapefiles as a list: library(rgdal) library(raster) library(sf) library(maptools) NOAA_normal <- raster("C:/path/to/raster/noaa_normal.tif") input_path <- "C:/path/to/Counties" files <- list.files(dir, pattern="[.]shp$", full.names=TRUE)

Creating R loop to read in shapefiles from a directory and perform zonal statistics on each

↘锁芯ラ 提交于 2020-01-16 09:41:22
问题 I have 120 county shapefiles in a directory "Counties". I want to use R to read in each shapefile and, for each shapefile, perform zonal statistics (mean) using a single raster layer "NOAA_normal_crop." I was able to create a script that reads in all of the shapefiles as a list: library(rgdal) library(raster) library(sf) library(maptools) NOAA_normal <- raster("C:/path/to/raster/noaa_normal.tif") input_path <- "C:/path/to/Counties" files <- list.files(dir, pattern="[.]shp$", full.names=TRUE)

Creating R loop to read in shapefiles from a directory and perform zonal statistics on each

本小妞迷上赌 提交于 2020-01-16 09:41:09
问题 I have 120 county shapefiles in a directory "Counties". I want to use R to read in each shapefile and, for each shapefile, perform zonal statistics (mean) using a single raster layer "NOAA_normal_crop." I was able to create a script that reads in all of the shapefiles as a list: library(rgdal) library(raster) library(sf) library(maptools) NOAA_normal <- raster("C:/path/to/raster/noaa_normal.tif") input_path <- "C:/path/to/Counties" files <- list.files(dir, pattern="[.]shp$", full.names=TRUE)

SparkR foreach loop

最后都变了- 提交于 2020-01-15 03:26:14
问题 In Java/Scala/Python implementations of Spark, one can simply call the foreach method of RDD or DataFrame types in order to parallelize the iterations over a dataset. In SparkR I can't find such instruction. What would be the proper way to iterate over the rows of a DataFrame ? I could only find the gapply and dapply functions, but I don't want to calculate new column values, I just want to do something by taking one element from a list, in parallel. My previous attempt was with lapply

Why do I need to wrap `get` in a dummy function within a J `lapply` call?

三世轮回 提交于 2020-01-14 14:30:30
问题 I'm looking to process columns by criteria like class or common pattern matching via grep . My first attempt did not work: require(data.table) test.table <- data.table(a=1:10,ab=1:10,b=101:110) ##this does not work and hangs on my machine test.table[,lapply(names(test.table)[grep("a",names(test.table))], get)] Ricardo Saporta notes in an answer that you can use this construct, but you have to wrap get in a dummy function: ##this works test.table[,lapply(names(test.table)[grep("a",names(test