lapply

Unexpected behavior of kable when called from lapply or from function with print statement

拟墨画扇 提交于 2019-12-09 16:53:33
问题 I am trying to understand the two following unexpected behaviors of the kable function when knitting HTML using the knitr package (in RStudio 0.98.977 on Ubuntu 14.04): When two calls of kable are made from within lapply, only the first call produces a pretty display in the final HTML. When two calls of kable are made from within a function that also uses print statements, only the last call produces a pretty display in the final HTML. An example code is written below: Load library: ```{r

data.table grouping separately on numeric and text variables

北慕城南 提交于 2019-12-08 17:09:57
问题 I'm trying to simplify this data.table two-stage process which acts on both numeric and character variables. E.g. - take the first element of textvar and sum each of the numeric variables. Consider this small example: library(data.table) dt <- data.table(grpvar=letters[c(1,1,2)], textvar=c("one","two","one"), numvar=1:3, othernum=2:4) dt # grpvar textvar numvar othernum #1: a one 1 2 #2: a two 2 3 #3: b one 3 4 Now my first thought was to nest .SD to drop the one variable out of the lapply

Use a weights argument in a list of lm lapply calls [duplicate]

一曲冷凌霜 提交于 2019-12-08 16:43:40
问题 This question already has answers here : Error in calling `lm` in a `lapply` with `weights` argument (2 answers) Closed last year . Here is my problem (fictional data in order to be reproducible) : set.seed(42) df<-data.frame("x"=rnorm(1000),"y"=rnorm(1000),"z"=rnorm(1000)) df2<-data.frame("x"=rnorm(100),"y"=rnorm(100),"z"=rnorm(100)) breaks<-c(-1000,-0.68,-0.01315,0.664,1000) divider<-cut(df$x,breaks) divider2<-cut(df2$x,breaks) subDF<-by(df,INDICES=divider,data.frame) subDF2<-by(df2,INDICES

using lapply on a list of dataframes

拈花ヽ惹草 提交于 2019-12-08 12:37:09
问题 I created a list of dataframes called "list" and want to select only certain columns of every dataset in the list. library(dplyr) new_list <- lapply(list, select(list, Date)) It returns an error because class(list[1]) is not dataframe but still a list. class(list[[1]]) is dataframe. I don't understand that because the elements in my list should be dataframes and I also don't know how I can use "lapply" anyway. Thanks for your help! 回答1: I think your syntax is just a little off. Try using an

Arguments in read.delim when used in lapply

萝らか妹 提交于 2019-12-08 08:06:21
问题 I'm using the following code to read a set of CSV files: csvfiles = list.files(pattern = "*.csv", path = "./Data/", full.names = TRUE) ## Merge into one object myfiles = lapply(csvfiles, read.delim) I would like to pass some arguments to the read.delim function in order to skip a set of initial rows, define delimiter and so on but whenever I try doing this R returns an error, as illustrated below: > myfiles = lapply(csvfiles, read.delim(skip = 2)) Error in read.table(file = file, header =

reduce row to unique items

蓝咒 提交于 2019-12-08 07:00:29
问题 I have the dataframe test <- structure(list( y2002 = c("freshman","freshman","freshman","sophomore","sophomore","senior"), y2003 = c("freshman","junior","junior","sophomore","sophomore","senior"), y2004 = c("junior","sophomore","sophomore","senior","senior",NA), y2005 = c("senior","senior","senior",NA, NA, NA)), .Names = c("2002","2003","2004","2005"), row.names = c(c(1:6)), class = "data.frame") > test 2002 2003 2004 2005 1 freshman freshman junior senior 2 freshman junior sophomore senior 3

extracting one set of multiple variables in a list of data.frames in R

可紊 提交于 2019-12-08 06:45:22
问题 Suppose I have a data.frame like THIS . Any columns of data after the column named autoreg are arbitrary columns defined by the user. So, I won't know the columns names or values. For example, in THIS data.frame columns named: "ESL" "prof" "scope" "type" are defined by the user. Question: How can I have a looping structure (in BASE R) that at each round, extracts one set of each of these arbitrary columns? My desired output is a list within which the ESL values prof values scope values and

Looping multiple listed data frames into a single function

守給你的承諾、 提交于 2019-12-08 05:42:28
问题 I am trying to execute the function varipart() from the package ade4. I am trying to use the same number dataframe from each list in the different parts of the same function. I need to pass this for each set of dataframes. ########### DATA BELOW d1 <- data.frame(y1 = c(1, 2, 3), y2 = c(4, 5, 6)) d2 <- data.frame(y1 = c(3, 2, 1), y2 = c(6, 5, 4)) d3 <- data.frame(y1 = c(2, 1, 2), y2 = c(5, 6, 4)) spec.list <- list(d1, d2, d3) d1 <- data.frame(y1 = c(20, 87, 39), y2 = c(46, 51, 8)) d2 <- data

Saving and accessing results from regression in a loop

佐手、 提交于 2019-12-08 05:28:49
问题 I am trying to do several panel data regression through the pml package in a for loop and then save the results, so that I can use summary on each of the regression results. However, I can't seem to figure out how to use summary on the list of saved results. This is what I have tried: library(plm) ########### Some toy data ################# Id <- c(rep(1:4,3),rep(5,2)) Id <- Id[order(Id)] Year <- c(rep(2000:2002,4),c(2000,2002)) z1 <- rnorm(14) z2 <- rnorm(14) z3 <- rnorm(14) z4 <- rnorm(14)

Apply vs For loop in R

余生颓废 提交于 2019-12-08 03:51:55
问题 I wrote the following code to scrap tendering information from a portal on daily basis. packages <- c('rvest', 'stringi', 'tidyverse','lubridate','dplyr') purrr::walk(packages, library, character.only = TRUE, warn.conflicts = FALSE) start_time <- proc.time() Main Page to scrap and get total no of records. data <- read_html('https://eprocure.gov.in/mmp/latestactivetenders') total_tenders_raw <- html_nodes(data,xpath = '//*[(@id = "table")]') All_tenders <- data.frame(html_table(total_tenders