lapply

How to write a result of function in the global environment in R

南楼画角 提交于 2019-12-24 02:16:43
问题 I have these datasets: A <- 4, B <- 3, C <- 2 . So I put them in a list D<-list(A,B,C) and want to apply this function: s<-function(x) { t<-8 x<-as.data.frame(x*t) } lapply(D,s) when I apply the lapply function it just print them. How can I make it saving the result in the global environment instead of printing them? So the result should be A with value of 32 B with value of 24 C with value of 16. 回答1: Instead of lapply(D,s) , use: D <- lapply(D, s) names(D) <- c("A", "B", "C") list2env(D,

How to create a new column with names in a list

扶醉桌前 提交于 2019-12-24 01:47:13
问题 I have searched posts on web to find a solution. But, I could not identify any. Therefore, I decided to ask your help. I have a list with data frames. I chose certain columns from each data frame and combine them. When I was combining data from two data frames, I wanted to add a column which includes the names of the list. But, I could not achieve this. Here are a sample data and what I have tried. Sample data & my attempt ### 1st dataframe time <- seq(as.Date("2014-09-01"), by = "day",

Can you use the lapply() function to alter the value of input?

倾然丶 夕夏残阳落幕 提交于 2019-12-24 01:08:21
问题 I was wondering whether it is possible to use the lapply() function to alter the value of the input, similar to: a1<-runif(100) a2<-function(i){ a1[i]<-a1[i-1]*a1[i];a1[i] } a3<-lapply(2:100,a2) I'm looking for something akin to a for() loop, but using the lapply() infrastructure. I haven't been able to get rapply() to do this. The reason is that the "real" a2 function is a difficult function that only needs to be evaluated if the value of a1[i-1] meets some criteria. re-phrasing: so i'm

Reorder words in each element of a vector

最后都变了- 提交于 2019-12-24 00:27:04
问题 I'd like to change the word order for each element in a vector. Specifically I'd like to make another vector where the first word is now the last word for a number of elements that differ in length. Data metadata1 <- c("reference1 an organism", "reference2 another organism here", "reference3 yet another organism is here") Desired outcome metadata2 <- c("an organism reference1", "another organism here reference2", "yet another organism is here reference3") My attempt metadata2 <- lapply

How can I transform an array of characters with a few lines of code to a data.frame?

℡╲_俬逩灬. 提交于 2019-12-23 19:19:57
问题 I have the following array my_list <- c("Jan-01--Dec-31|00:00--24:00", "Jan-01--Jun-30|12:00--18:00", "Jul-06--Dec-31|09:00--19:00") What is the shortest code which results in: x1 x2 x3 1 Jan-01 Jan-01 Jul-06 2 Dec-31 Jun-30 Dec-31 and x2 x2 x3 1 00:00 12:00 09:00 2 24:00 18:00 19:00 At the moment I have the (not very nice) code df <- as.data.frame(strsplit(my_list, split = "|", fixed = T), stringsAsFactors = F) date_list <- strsplit(as.character(df[1, ]), split = "--", fixed = T) date_df <-

get the lengths of element of lists of list in R

回眸只為那壹抹淺笑 提交于 2019-12-23 19:18:55
问题 Here is my list called dico : dico <- list(list(list(c("dim.", "dimension", "dimensions", "mesures" ), c("45 cm", "45", "45 CM", "0.45m")), list(c("tamano", "volumen", "dimension", "talla"), c("45 cm", "45", "0.45 M", "45 centimiento" )), list(c("measures", "dimension", "measurement"), c("45 cm", "0.45 m", "100 inches", "100 pouces"))), list(list(c("poids", "poid", "poids net"), c("100 grammes", "100 gr", "100")), list( c("peso", "carga", "peso especifico"), c("100 gramos", "100g", "100",

Access to column name of dataframe with *apply function

北慕城南 提交于 2019-12-23 16:40:13
问题 I need to make tutorial for beginner using the R *apply function (without using reshape or plyr package in a first time) I try to lapply (because i read apply is not good for dataframe) a simple function to this dataframe, and i want to use named column to access data : fDist <- function(x1,x2,y1,y2) { return (0.1*((x1 - x2)^2 + (y1-y2)^2)^0.5) } data <- read.table(textConnection("X1 Y1 X2 Y2 1 3.5 2.1 4.1 2.9 2 3.1 1.2 0.8 4.3 ")) data$dist <- lapply(data,function(df) {fDist(df$X1 , df$X2 ,

R - slowly working lapply with sort on ordered factor

痴心易碎 提交于 2019-12-23 15:56:48
问题 Based on the question More efficient means of creating a corpus and DTM I've prepared my own method for building a Term Document Matrix from a large corpus which (I hope) do not require Terms x Documents memory. sparseTDM <- function(vc){ id = unlist(lapply(vc, function(x){x$meta$id})) content = unlist(lapply(vc, function(x){x$content})) out = strsplit(content, "\\s", perl = T) names(out) = id lev.terms = sort(unique(unlist(out))) lev.docs = id v1 = lapply( out, function(x, lev) { sort(as

R - slowly working lapply with sort on ordered factor

≡放荡痞女 提交于 2019-12-23 15:54:04
问题 Based on the question More efficient means of creating a corpus and DTM I've prepared my own method for building a Term Document Matrix from a large corpus which (I hope) do not require Terms x Documents memory. sparseTDM <- function(vc){ id = unlist(lapply(vc, function(x){x$meta$id})) content = unlist(lapply(vc, function(x){x$content})) out = strsplit(content, "\\s", perl = T) names(out) = id lev.terms = sort(unique(unlist(out))) lev.docs = id v1 = lapply( out, function(x, lev) { sort(as

data.table: Using with=False and transforming function/summary function?

筅森魡賤 提交于 2019-12-23 10:17:04
问题 I want to summarise several variables in data.table, output in wide format, output possibly as a list per variable. Since several other approaches did not work, I tried to do an outer lapply, giving the names of the variables as character vectors. I wanted to pass these in, using with=FALSE. carsx=as.data.table(cars) lapply( list(speed="speed",dist= "dist"), #error object 'ansvals' not found function(x) carsx[,list(mean(x), min(x), max(x) ), with=FALSE ] ) Since this does not work, I tried