lapply

recode/replace multiple values in a shared data column to a single value across data frames

放肆的年华 提交于 2019-12-05 16:00:27
I hope I haven't missed it, but I haven't been able to find a working solution to this problem. I have a set of data frames with a shared column. These columns contain multiple and varying transcription errors, some of which are shared, others not, for multiple values. I would like replace/recode the transcription errors (bad_values) with the correct values (good_values) across all data frames. I have tried nesting the map*() family of functions across lists of data frames, bad_values, and good_values to do this, among other things. Here is an example: df1 = data.frame(grp = c("a1","a.","a."

Order a data frame in a list

心已入冬 提交于 2019-12-05 05:33:17
问题 If I have a list of data frames list.dfs <- list(df1 = data.frame(var1 = c(1:3), var2 = c(1:3), var3 = c(1:3)), df2 = data.frame(var1= c(1:3), var2 = c(1:3), var3 = c(1:3)), df3 = data.frame(var1= c(1:3), var2 = c(1:3), var3 = c(3:1))) How do I use lapply and order to sort every data frame in the list by var3 (lowest to highest) 回答1: lapply(list.dfs, function(x) x[order(x$var3), ]) will do the trick. 来源: https://stackoverflow.com/questions/21391377/order-a-data-frame-in-a-list

How to get the name of each element of a list using lapply()?

一世执手 提交于 2019-12-05 02:49:51
Imagine that I have the following list > test <- list("a" = 1, "b" = 2) Each element of the list has a name : > names(test) Now, I want to extract that name using lapply() because I want to use it in a new function which will be called using lapply. I just don't know how to extract the name of each element. I've tried using deparse() and substitute() but the outcome is weird : > lapply(test, function(x) {deparse(substitute(x))}) $a [1] "X[[i]]" $b [1] "X[[i]]" Does anyone has a clue ? Precision : I want to do something like this : I have a list which is like test : > test <- list("a" = matrix

Convert for loop to apply

十年热恋 提交于 2019-12-05 02:28:04
问题 In R, how do you replace the following code using functions like apply , lapply , rapply , do.call , etc.? u <- 10:12 slist <- list() for (i in 1:length(u)) { p <- combn(u, i) for (j in 1:ncol(p)) { s <- paste(p[,j], collapse=",") slist[[s]] <- 0 } } For this part: for (j in 1:ncol(p)) { s <- paste(p[,j], collapse=",") I tried something like: s <- apply(p, 2, function(x) paste(x, collapse=",")) Which works. But then for that slist[[s]] <- 0 part inside that same for-loop, I don't know what to

How to subset a list using another list?

大憨熊 提交于 2019-12-04 20:02:40
I have two lists and I want to subset data in a list using another list. Say, I have lists called mylist and hislist : mylist <- list(a = data.frame(cola = 1:3, colb = 4:6), b = data.frame(cola = 1:3, colb = 6:8)) > mylist $a cola colb 1 1 4 2 2 5 3 3 6 $b cola colb 1 1 6 2 2 7 3 3 8 > and hislist hislist <- list(a = 5:6, b = 7:8) > hislist $a [1] 5 6 $b [1] 7 8 I tried to subset mylist using lapply function: lapply(mylist, function(x) subset(x, colb %in% hislist)) #or lapply(mylist, function(x) x[x$colb %in% hislist,]) but these does not work. How to solve this issue? The most straightforward

R Markdown Shiny renderPlot list of plots from lapply

雨燕双飞 提交于 2019-12-04 19:41:26
I am developing an R Markdown Shiny document to: Subset a data frame to include the "date" column and some numeric data columns. The way the shiny user input is set up, you select radio buttons for the data columns to include, then hit the "Subset Data" button to create d() - NO PROBLEM:) Generate a list of plots ( plotList ), one for each numeric data column (plotted against the date column). I am using the openair package timePlot function to generate the plots, and lapply to generate the list of plot objects ( plotList ) - NO PROBLEM:) use renderPlot to output all the plots in plotList to

R - How to vectorize with apply family function and avoid while/for loops in this case?

时光怂恿深爱的人放手 提交于 2019-12-04 19:23:43
In this case (more details could be found in this question: Count how many observations in the rest of the dat fits multiple conditions? (R) ) This is a dataset called event, containing thousands of events (observations) and I selected several rows to show you the data structure. It contains the "STATEid", "date" of occurrence, and geographical coordinates in two variables "LON" "LAT". I am writing to calculate a new variable (column) for each row. This new variable should be: "Given any specific incident, count the rest of the dataset and calculate the number of events that's happened in the

How to batch process geoTIFFs in R with lapply

余生颓废 提交于 2019-12-04 17:12:31
I have some large geoTIFFs, now I want to convert them to ASCII files, after doing some searches, I write these codes: library(raster) f <- list.files("inputFolder", pattern = "*.tif", full.names = TRUE) r <- lapply(f, raster) a <- lapply(r, writeRaster, filename = "output", format = "ascii") What confused me is that how can I name the output files respectively, according to its original names? I tried: a <- lapply(r, writeRaster, filename = "outputFolder" + f, format = "ascii") But I received error: non-numeric argument to binary operator Then I tried: a <- lapply(r, writeRaster, filename =

How to observe dynamically rendered ui

浪尽此生 提交于 2019-12-04 16:58:09
So, you solve one problem and run into the next. I've now succesfully build a part of code that dynamically creates uiOutput, i.e. a number of sliders, buttons and/or textfields and the amount of them depends on a value that comes rolling out of my model in an ealier step. However, I'm pretty clueless how to observe whether they are "clicked" / "changed" by the user. Lets say, the model gives out a nr 12 then the server tells my ui to make 12 buttons. I want to know when the user pushes ANY of the buttons and WHICH button it is To give a clear example in words: if user clicks button 8, i want

Creating multiple dummies from an existing data frame or data table

China☆狼群 提交于 2019-12-04 14:04:50
I am looking for a quick extension to the following solution posted here . In it Frank shows that for an example data table test <- data.table("index"=rep(letters[1:10],100),"var1"=rnorm(1000,0,1)) You can quickly make dummies by using the following code inds <- unique(test$index) ; test[,(inds):=lapply(inds,function(x)index==x)] Now I want to extend this solution for a data.table that has multiple rows of indices, e.g. new <- data.table("id" = rep(c("Jan","James","Dirk","Harry","Cindy","Leslie","John","Frank"),125), "index1"=rep(letters[1:5],200),"index2" = rep(letters[6:15],100),"index3" =