lapply

Extract data from list of lists [R]

╄→尐↘猪︶ㄣ 提交于 2019-12-24 16:09:29
问题 I have a list of lists (mydata$notes) that I want to extract data from. Code looks like this, if I want to extract "location" - this works fine. location <- unlist (lapply(mydata$notes, function(e) e$location)) Now, I might have more variables I want to extract, say a vector of 20, "location", "var1", "var2", "var3" and so on, in an atomic vector names(unlist(mytree$notes[[1]])) How can I loop my first code to extract all variables given in this names-variable? Cheers 回答1: Define a vector to

data.frame colmn mean at n-th interval of rows

半城伤御伤魂 提交于 2019-12-24 14:31:45
问题 I tried to find similar questions here but thought that it might be helpful to just create new question. I have relatively large dataset (daily weather data for several hundreds stations) I did some sliding window average calculation and I got the intermediate output like this. data frame of 10481441 row by 11 column but my row names are consist of series of numbers with identifiable prefix. For example, X1.1 X1.2 X1.3 ... X1.11659 X2.1 X2.2 X2.3 ... X2.11659 X3.1 ... X899.11659 Then what I

Select last non-NA column of a list of dataframes

白昼怎懂夜的黑 提交于 2019-12-24 12:19:12
问题 I am currently trying to parse RDP multiclassifier hierarchy files in R, however the problem is more generally applicable. Basically I create a list which contains data-frames of several files which contain "hierarchical" rows: dput(corner(hierlist$hier_M2MID06_Trimmed_noGaps.fas_fixrank.txt,n=c(7,10))) structure(list(X1 = structure(c(30L, 31L, 163L, 45L, 64L, 65L, 66L), .Label = c("-1071", "-1102", "-1153", "-1159", "-1176", "-1177", "-1207", "-1241", "-1256", "-1281", "-1332", "-1353", "

Empty rows in list as NA values in data.frame in R

邮差的信 提交于 2019-12-24 11:33:54
问题 I have a dataframe as follows: hospital <- c("PROVIDENCE ALASKA MEDICAL CENTER", "ALASKA REGIONAL HOSPITAL", "FAIRBANKS MEMORIAL HOSPITAL", "CRESTWOOD MEDICAL CENTER", "BAPTIST MEDICAL CENTER EAST", "ARKANSAS HEART HOSPITAL", "MEDICAL CENTER NORTH LITTLE ROCK", "CRITTENDEN MEMORIAL HOSPITAL") state <- c("AK", "AK", "AK", "AL", "AL", "AR", "AR", "AR") rank <- c(1,2,3,1,2,1,2,3) df <- data.frame(hospital, state, rank) df hospital state rank 1 PROVIDENCE ALASKA MEDICAL CENTER AK 1 2 ALASKA

3 layer Stacked histogram from already summarized counts using ggplot2

喜你入骨 提交于 2019-12-24 11:09:09
问题 I would like some help coloring a ggplot2 histogram generated from summarized data in a data.frame. The dataset I'm using is the [R] build in (USArrests) dataset. I'm trying to adapt the solution that was given to this question by arun. The desired result is to make a histogram of "Crime" and color each bar according to the relative contribution of c("Assault", "Rape", "Murder"). The code: attach(USArrests) #Create vector SUM arrests per state Crime <- with(USArrests, Murder+ Rape+ Assault)

Avoiding lapply() in R, and finding all elements of Vector B that meet a condition of for each element of Vector A

橙三吉。 提交于 2019-12-24 11:02:37
问题 I have two vectors. For each element of vector A , I would like to know all the elements of vector B that fulfill a certain condition. So, for example, two dataframes containing the vectors: person <- data.frame(name = c("Albert", "Becca", "Celine", "Dagwood"), tickets = c(20, 24, 16, 17)) prize <- data.frame(type = c("potato", "lollipop", "yo-yo", "stickyhand", "moodring", "figurine", "whistle", "saxophone"), cost = c(6, 11, 13, 17, 21, 23, 25, 30)) For this example, each person in the

Change level of multiple factor variables

狂风中的少年 提交于 2019-12-24 10:57:31
问题 everyone - I want to preface this by saying that I already looked at this link to try to solve my problem: Applying the same factor levels to multiple variables in an R data frame The difference is that in that problem, the OP wanted to change the levels of factors that all had the same levels. In my instance, I'm looking to change just the first level, which is set to ' ', to something like 'Unknown' and leave the rest of the levels alone. I know I could do this in a "non-R" way with

Calling update within a lapply within a function, why isn't it working?

試著忘記壹切 提交于 2019-12-24 09:43:26
问题 This a a follow up question from Error in calling `lm` in a `lapply` with `weights` argument but it may not be the same problem (but still related). Here is a reproducible example: dd <- data.frame(y = rnorm(100), x1 = rnorm(100), x2 = rnorm(100), x3 = rnorm(100), x4 = rnorm(100), wg = runif(100,1,100)) ls.form <- list( formula(y~x1+x2), formula(y~x3+x4), formula(y~x1|x2|x3), formula(y~x1+x2+x3+x4) ) I have a function that takes different arguments (1- a subsample, 2- a colname for the

dplyr, lapply, or Map to identify information from one data.frame and place it into another [duplicate]

大城市里の小女人 提交于 2019-12-24 06:33:58
问题 This question already has answers here : How to join (merge) data frames (inner, outer, left, right) (13 answers) Closed 3 years ago . edit: Sorry y'all, I didn't mean to repost a question. The issue I'm having isn't just with joining two tables, it's joining two tables with a column that isn't exactly the same in both tables (I updated the sample data to illustrate this). That is, I want to pmatch, or str_detect the strings within the Test.Takers$First column with the Every.Student.In.The

Split Data Frame for passing to sprintf in R

谁说胖子不能爱 提交于 2019-12-24 03:00:38
问题 I have a sprintf format string that I'm trying to parse a dataframe in R through. I built this code to do it, but it is nothing if not ugly. What's a better way to do this? writeData<-function(DataSet,FirstLine,FmtString,fName){ correctLine<-function (MyLine,FmtString){ do.call(sprintf,c(FmtString,MyLine)) } #why the ugly split code? Because otherwise it casts my nice data frame as characters which confuses sprintf. outLines=lapply(split(DataSet,1:NROW(DataSet)),function (x){correctLine(x