lapply

reduce row to unique items

安稳与你 提交于 2019-12-06 16:45:11
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 freshman junior sophomore senior 4 sophomore sophomore senior <NA> 5 sophomore sophomore senior <NA> 6

How to subset a list using another list?

怎甘沉沦 提交于 2019-12-06 15:02:22
问题 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

Modify and recreate a list of data.frame in R

守給你的承諾、 提交于 2019-12-06 13:57:34
问题 I have a list of data.frame s called m (see HERE). Column r in these data.frames is all NA . But later on, I have computed some of these r s and stored them as a list called L . I'm wondering how to achieve the following?: (1) If any list entry in L (i.e., L[[1]] , L[[2]] , ...), starts with a number BUT right after it is NA , replace NA with that number. (2) Put back all new r s (stored in L ) in column r , in the original list of data.frames m . D <- read.csv("https://raw.githubusercontent

Using R functions lapply and read.sql.csv

风流意气都作罢 提交于 2019-12-06 13:27:27
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 of functionality into a single command such that i can read all the files in a directory applying the

R lapply statement with index [duplicate]

雨燕双飞 提交于 2019-12-06 12:24:21
This question already has answers here : Access lapply index names inside FUN (12 answers) Closed 4 years ago . Is there a way I can make an lapply statement also show the index? More specifically, consider the following example: mylist <- list(c(5,4),c(3,2), c(1,3)) myfunction<- function(values){ print("Adding values: ") return(values[1] + values[2]) } lapply(mylist, myfunction) Is there a way I can somehow make it print "Adding Values: 1" "Adding Values: 2" etc.. one element of each in the list? Thanks! The function mapply works like lapply but allows you to supply multiple vectors or lists.

“undefined columns selected” - when trying to remove na's from df's in list

﹥>﹥吖頭↗ 提交于 2019-12-06 11:57:45
I am trying to replicate the success of this solution: remove columns with NAs from all dataframes in list or Remove columns from dataframe where some of values are NA with a list of dataframes: m1<- structure(list(vPWMETRO = c(1520L, 1520L, 1520L, 1520L, 1520L), vPWPUMA00 = c(500L, 900L, 1000L, 1100L, 1200L), v100 = c(96.1666666666667, 71.4615384615385, 68.6363636363636, 22.5, 64.5), v101 = c(5, 15, NA, NA, NA), v102 = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_)), .Names = c("vPWMETRO", "vPWPUMA00", "v100", "v101", "v102"), row.names = 26:30, class = "data.frame") m2<- structure(list

Apply an already defined function to all dataframes at once

让人想犯罪 __ 提交于 2019-12-06 11:53:21
I already have defined a function (which works fine). Nevertheless, I have 20 dataframes in the working space to which I want to lapply the same function (dat1 to dat20). So far it looks like this: dat1 <- func(dat=dat1) dat2 <- func(dat=dat2) dat3 <- func(dat=dat3) dat4 <- func(dat=dat4) ... dat20 <- func(dat=dat20) However, is there a way to do this more elegant with a shorter command, i.e. to lapply the function to all dataframes at once? I tried this, but it didn't work: mylist <- paste0("dat", 1:20, sep="") lapply(mylist, func) Try something like: lapply(mget(ls(pattern="dat")),func) Some

Use variable name as plot title with lapply

拜拜、爱过 提交于 2019-12-06 11:01:20
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.arrange(g1,g2,g3) But this becomes inefficient when I need to include conditional statements if there are

Work with data frames stored in lists

夙愿已清 提交于 2019-12-06 10:36:06
问题 With this question I would like to extend and generalize the discussion started here. This is for the benefit of those, like me, who are still in trouble when have to use lapply. Suppose I have the data frames d1 and d2 which I store in the list my.ls d1<-data.frame(a=rnorm(5), b=c(rep(2006, times=4),NA), c=letters[1:5]) d2<-data.frame(a=1:5, b=c(2007, 2007, NA, NA, 2007), c=letters[6:10]) my.ls<-list(d1=d1, d2=d2) How can I obtain another list featuring the same data frames for which I keep

Subtracting similarly named elements in a list of vectors in R

て烟熏妆下的殇ゞ 提交于 2019-12-06 08:04:58
问题 I have a list of 3 vectors, X , Y , and Z . I want to subtract similarly named elements of these 3 vectors. That is, similarly named elements subtracted throughout like so: X - Y - Z . Also, if an element (here ChandlerA and Trus.Hsu in X ) appears only once in one vector but not in others, then I want to skip that element altogether. My desired output is: c(Bit.KnoA = -2, Bit.KnoB = -4, Bit.KnoC = -2, Ellis.etal = 3, Mubarak = 3, sheenA = 5, Shin.Ellis = 5, Sun = 7) Is it possible to achieve