lapply

obtaining average coefficients and adj. R^2 from multiple pooled regressions using lapply

天涯浪子 提交于 2020-01-13 19:23:09
问题 I have performed multiple Pooled regressions with a loop function and stored the regression output in a list (myregression). What i would like to do now is to efficiently perform the coeftest function in the lmtest package over all my regressions (i.e. myregression list) to adjust standard errors and t-statistics. Finally i would like to obtain the mean of the coefficients, standard errors and t-values. Here is what i came up so far: library(plm) data("Grunfeld", package="plm") # Store each

Use variable name as plot title with lapply

两盒软妹~` 提交于 2020-01-13 19:13:29
问题 I need to display between 1-3 graphs, and I want the titles of the graphs to be based on the variable name that used. I can get this to work how I want below: library(grid) library(ggplot2) library(gridExtra) a1 <- sample(1:100) a2 <- sample(1:100) a3 <- sample(1:100) make_graph <- function(x, y=deparse(substitute(x))){ time <- 1:100 dfp <- data.frame(time, x) ggplot(dfp, aes(x=time, y=x)) + geom_point() + ggtitle(y) } g1 <- make_graph(a1) g2 <- make_graph(a2) g3 <- make_graph(a3) grid

Use variable name as plot title with lapply

空扰寡人 提交于 2020-01-13 19:13:26
问题 I need to display between 1-3 graphs, and I want the titles of the graphs to be based on the variable name that used. I can get this to work how I want below: library(grid) library(ggplot2) library(gridExtra) a1 <- sample(1:100) a2 <- sample(1:100) a3 <- sample(1:100) make_graph <- function(x, y=deparse(substitute(x))){ time <- 1:100 dfp <- data.frame(time, x) ggplot(dfp, aes(x=time, y=x)) + geom_point() + ggtitle(y) } g1 <- make_graph(a1) g2 <- make_graph(a2) g3 <- make_graph(a3) grid

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

元气小坏坏 提交于 2020-01-13 11:23:10
问题 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

Dollar operator as function argument for sapply not working as expected

混江龙づ霸主 提交于 2020-01-12 06:57:21
问题 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? 回答1: From what I've

Converting columns to character with sapply

蹲街弑〆低调 提交于 2020-01-07 08:25:11
问题 I have a dataframe test whose columns are factors class(test) [1] "data.frame" sapply(test, class) street city state "factor" "factor" "factor" If I try to convert these columns to character with sapply() , something goes wrong and I not sure why test <- as.data.frame(sapply(test, as.character)) sapply(test, class) street city state "factor" "factor" "factor" I would expect the output to be all character columns. Why are the columns not converting and how would one convert all factor columns

Sequence of execution for closures

纵饮孤独 提交于 2020-01-07 03:48:23
问题 Here's a test function I created to learn how lapply and closures interact. My objective is to iterate over runif function sequentially and print each iteration. Mod_delay_by <- function(delay,f){ function(x,...){ Sys.sleep(delay) cat(".") #f(x,...) cat("Processing",x,"\n",sep = "") f(x,...) } } Now let's use lapply to call above function: lapply(1:3,Mod_delay_by(0.1,runif)) When I call above function, I get the following sample output: .Processing1 .Processing2 .Processing3 [[1]] [1] 0

Speed up R script looping through files/folders to check thresholds, calculate averages, and plot

自古美人都是妖i 提交于 2020-01-07 03:10:29
问题 I'm trying to speed up some code in R. I think my looping methods can be replaced (maybe with some form of lapply or using sqldf) but I can't seem to figure out how. The basic premise is that I have a parent directory with ~50 subdirectories, and each of those subdirectories contains ~200 CSV files (a total of 10,000 CSVs). Each of those CSV files contains ~86,400 lines (data is daily by the second). The goal of the script is to calculate the mean and stdev for two intervals of time from each

t.test: create lapply function for multiple grouping levels

断了今生、忘了曾经 提交于 2020-01-06 12:42:47
问题 I'm trying to create an lapply function to run multiple t.test s for multiple levels of grouping. I came across this question: Kruskal-Wallis test: create lapply function to subset data.frame? but they were only trying to group by one variable ( phase ). I would like to add another grouping level color , where my iv is distance and dv is val grouped by color then phase . # create data val<-runif(60, min = 0, max = 100) distance<-floor(runif(60, min=1, max=3)) phase<-rep(c("a", "b", "c"), 20)

Loop or vector for creating multiple graphs for network analysis in R

六月ゝ 毕业季﹏ 提交于 2020-01-06 07:00:59
问题 I've created a graph from a dataframe with igraph that shows how students move between vocational courses. The first two columns are 'From' and 'To' which contain the qualification names (vertices) with an occurrence for each time a student enrols in a course for the first time (From) and then enrols in another (To). The remaining columns contain values relating to that particular student's demographics. I'd like to create and plot individual networks for movements 'To' OR 'From' each