lapply

Creating variables from factor records in R

拟墨画扇 提交于 2019-12-03 22:44:41
问题 I am kind of lost, I have a data frame that looks like this: tract ageClass count 1 [0-4] 71 2 [0-4] 192 3 [0-4] 81 1 [5-8] 9 2 [5-8] 86 3 [5-8] 42 I would like to have this result: tract [0-4] [5-8] 1 71 9 2 192 86 3 81 42 I have been looking in the internet for a solution for quite some time but so far nothing... any idea? Many thanks! 回答1: Three possible options I can think of (assuming your data set called df ) xtabs(count ~ tract + ageClass, df) # ageClass # tract [0-4] [5-8] # 1 71 9 #

Order a data frame in a list

╄→гoц情女王★ 提交于 2019-12-03 20:55:55
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) 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

Convert for loop to apply

人盡茶涼 提交于 2019-12-03 17:35:11
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 do. Edit: This is what I'm trying to do. For the vector u , I'm producing a list of all the subsets in

Why does lapply() not retain my data.table keys?

☆樱花仙子☆ 提交于 2019-12-03 11:49:58
I have a bunch of data.tables in a list. I want to apply unique() to each data.table in my list, but doing so destroys all my data.table keys. Here's an example: A <- data.table(a = rep(c("a","b"), each = 3), b = runif(6), key = "a") B <- data.table(x = runif(6), b = runif(6), key = "x") blah <- unique(A) Here, blah still has a key, and everything is right in the world: key(blah) # [1] "a" But if I add the data.tables to a list and use lapply() , the keys get destroyed: dt.list <- list(A, B) unique.list <- lapply(dt.list, unique) # Keys destroyed here lapply(unique.list, key) # [[1]] # NULL #

Dollar operator as function argument for sapply not working as expected

China☆狼群 提交于 2019-12-03 10:55:58
I have the following list test_list=list(list(a=1,b=2),list(a=3,b=4)) and I want to extract all elements with list element name a . I can do this via sapply(test_list,`[[`,"a") which gives me the correct result #[1] 1 3 When I try the same with Rs dollar operator $ , I get NULL sapply(test_list,`$`,"a") #[[1]] #NULL # #[[2]] #NULL However, if I use it on a single element of test_list it works as expected `$`(test_list[[1]],"a") #[1] 1 Am I missing something obvious here? NGaffney From what I've been able to determine it's a combination of two things. First, the second element of $ is matched

R: How to sum multiple columns of data frames in a list?

此生再无相见时 提交于 2019-12-02 23:47:13
问题 i want to sum multiple columns of data frames in a list and only show the sum without showing the (calculation) input columns. Here an example: ls <- list(data.frame(a=1, b=5, c=3, d=2), data.frame(a=NA, b=2, c=7, d=9)) ls [[1]] a b c d 1 1 5 3 2 [[2]] a b c d 1 NA 2 7 9 my expected result is: ls2 [[1]] c new 1 3 8 [[2]] c new 1 7 11 Any ideas how to do this? So far I tried to enhance this answer for lists, without success and without omiting the input columns (a,b,d). I tried so far lapply:

Printing a column name inside lapply function

对着背影说爱祢 提交于 2019-12-02 12:35:13
问题 I have searched through the archives but have not found a suitable answer. I am a beginner and please excuse my ignorance if I am posing a very elementary query. I am trying to get the apply function to print the column names while processing through a data frame. I understand that lapply converts the column of data frame to vector, but is their way to print the column name while printing output. Like in the following example > mydata<-data.frame(matrix(rep(c(1:2),times= 50),20,5)) > colnames

Running lagged regressions with lapply and two arguments

喜欢而已 提交于 2019-12-02 10:23:37
问题 I am running multiple univariate regressions, like in this reproducible example: require(dynlm) data(USeconomic) US<-USeconomic vars<-colnames(US)[-2] a<-lapply(colnames(US),function(x) dynlm(log(GNP)~get(x),data=US)) a contains a list of 3 univariate regressions. Assume now I´d like to run the same regressions with 3 lags: l<-c(0,1,4) where 0 is of course the case I already got. Is there a way to use the vector l directly, like # this did not work for me, I obtain multivariate regressions

Aggregate sum obs with different ID's in the same data frame

南楼画角 提交于 2019-12-02 09:12:35
My goal is to make another column by summing the observation from the present day and all previous observations from the same ID by using the date (the data set is sorted in date and chr nr(ID). I will need the aggregation to start over when a new "id" is presented. there might be som NA's, they should be considered as null "Doseringer_pr_kg_dyr" is the observation. CHR_NR DATO_AFSLUT Doseringer_pr_kg_dyr brugstid 10358 2018-08-06 29416.67 31 10358 2018-09-06 104682.27 36 10358 2018-10-12 10333.33 26 10358 2018-11-07 10090.91 27 10358 2018-12-04 8000.00 NA 13168 2012-01-23 12042.25 2 13168

How to make a function in a for loop or lapply loop in a tabItem dashboard shiny

眉间皱痕 提交于 2019-12-02 07:49:25
问题 I'm making a ShinyDashboard program and I have some troubles in finding a way to make a loop in the dashboardBody to catch MenuItems. This is a simple example of what I'm trying to fix: library(shiny) library(shinyjs) library(shinydashboard) VecNames=c("A","B","C","D","E") ui <- dashboardPage( dashboardHeader(title = "My Page"), dashboardSidebar(sidebarMenuOutput("sideBar_menu_UI")), dashboardBody( uiOutput("body_UI"), uiOutput("test_UI") ) ) server <- shinyServer(function(input, output,