lapply

Lapply in a dataframe over different variables using filters

*爱你&永不变心* 提交于 2019-12-23 09:27:23
问题 I'm trying to calculate several new variables in my dataframe. Take initial values for example: Say I have: Dataset <- data.frame(time=rep(c(1990:1992),2), geo=c(rep("AT",3),rep("DE",3)),var1=c(1:6), var2=c(7:12)) time geo var1 var2 1 1990 AT 1 7 2 1991 AT 2 8 3 1992 AT 3 9 4 1990 DE 4 10 5 1991 DE 5 11 6 1992 DE 6 12 And I want: time geo var1 var2 var1_1990 var1_1991 var2_1990 var2_1991 1 1990 AT 1 7 1 2 7 8 2 1991 AT 2 8 1 2 7 8 3 1992 AT 3 9 1 2 7 8 4 1990 DE 4 10 4 5 10 11 5 1991 DE 5 11

Saving deeply nested files to specific directories with specific filenames

风格不统一 提交于 2019-12-23 04:35:07
问题 Given a 3 level nested list: mylist <- list("1000"=list("cars"=list("fast"=mtcars[1:10,], "slow"=mtcars[11:15,]), "flower"=iris), "2000"=list("tooth"=ToothGrowth, "air"=airquality, "cars"=list("cruiser"=mtcars[5:12,], "fast"=mtcars[1:3,], "mild"=mtcars[9:18,]))) (ie: mylist$1000$cars$fast , where fast is a dataframe, and cars and 1000 are nested lists in mylist ) I'd like to save each innermost dataframe, (ie: fast ) as a .csv with the df name as it's file name, ie: fast.csv , and I want the

Saving deeply nested files to specific directories with specific filenames

我与影子孤独终老i 提交于 2019-12-23 04:34:02
问题 Given a 3 level nested list: mylist <- list("1000"=list("cars"=list("fast"=mtcars[1:10,], "slow"=mtcars[11:15,]), "flower"=iris), "2000"=list("tooth"=ToothGrowth, "air"=airquality, "cars"=list("cruiser"=mtcars[5:12,], "fast"=mtcars[1:3,], "mild"=mtcars[9:18,]))) (ie: mylist$1000$cars$fast , where fast is a dataframe, and cars and 1000 are nested lists in mylist ) I'd like to save each innermost dataframe, (ie: fast ) as a .csv with the df name as it's file name, ie: fast.csv , and I want the

Count how many observations in the rest of the dat fits multiple conditions? (R)

流过昼夜 提交于 2019-12-23 04:26:39
问题 friends, I am new in R programming. I have been trying to write a user-defined function for days but not yet nailed it. 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

How do I pass column-specific arguments to lapply in data.table .SD?

微笑、不失礼 提交于 2019-12-23 02:55:18
问题 I have seen examples of using .SD with lapply in data.table with a simple function as below: DT[ , .(b,d,e) := lapply(.SD, tan), .SDcols = .(b,d,e)] But I'm unsure of how to use column-specific arguments in a multiple argument function. For instance I have a winsorize function, I want to apply it to a subset of columns in a data table but using column-specific percentiles, e.g. library(DescTools) wlevel <- list(b=list(lower=0.01,upper=0.99), c=list(upper=0.02,upper=0.95)) DT[ , .(b,c) :

Using R functions lapply and read.sql.csv

荒凉一梦 提交于 2019-12-22 11:22:57
问题 I am trying to open multiple csv files using a list such as the below; filenames <- list.files("temp", pattern="*.csv", full.names=TRUE) I have found examples that use lapply and read.csv to open all the files in the temp directory, but I know appriori what data i need to extract from the file, so to save time reading i want to use the SQL extension of this; somefile = read.csv.sql("temp/somefile.csv", sql="select * from file ",eol="\n") However i am having trouble combining these two pieces

How can I use attr<- with lapply?

落爺英雄遲暮 提交于 2019-12-22 10:28:50
问题 Or put it more general: How can I add multiple attributes to the elements of list? I am stuck trying to set an attribute to elements of a list all of which are data.frames . In the end I would like to add names(myList) as a varying attribute to every data.frame inside. But I even cannot get a static attribute for all list elements to go. lapply(myList,attr,which="myname") <- "myStaticName" This does not work because lapply does not work with lapply<- . If I had at least an idea how to do this

R: How to run function on two lists?

烈酒焚心 提交于 2019-12-22 10:05:56
问题 I want to run the following function on two lists: function(Z, p) { imp <- as.vector(cbind(imp=rowSums(Z))) exp <- as.vector(t(cbind(exp=colSums(Z)))) x = p + imp ac = p + imp - exp einsdurchx = 1/as.vector(x) einsdurchx[is.infinite(einsdurchx)] <- 0 A = Z %*% diag(einsdurchx) R = solve(diag(length(p))-A) %*% diag(p) C = ac * einsdurchx R_bar = diag(as.vector(C)) %*% R rR_bar = round(R_bar) return(rR_bar) } which works fine on a matrix and a vector . However, I need to run this function on a

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

≯℡__Kan透↙ 提交于 2019-12-22 03:36:15
问题 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 ?

R Markdown Shiny renderPlot list of plots from lapply

爷,独闯天下 提交于 2019-12-22 00:35:38
问题 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