sapply

Lists and matrix using sapply

我的未来我决定 提交于 2019-12-14 03:49:01
问题 I have a perhaps basic questions and I have searched on the web. I have a problem reading files. Though, I managed to get to read my files, following @Konrad suggestions, which I appreciate: How to get R to read in files from multiple subdirectories under one large directory? It is a similar problem, however, I have not resolved it. My problem: I have large number of files of with same name ("tempo.out") in different folders. This tempo.out has 5 columns/headers. And they are all the same

lapply function to compute means of data frames inside a list

拜拜、爱过 提交于 2019-12-13 11:19:45
问题 I am trying to compute means of data frames inside a list using lapply function. df_list <- list(cars, mtcars) sapply(df_list, mean) The above code doesn't seem to work. However when i changed it to: df_list <- c(cars, mtcars) sapply(df_list, mean) The output had the means of all the variables of both data frames. is there a way to compute the means using the first approach. 回答1: use the purrr library to achieve this...much simpler: library(purrr) map(df_list, ~map_if(., is.numeric, mean)) If

how paste function working in R? [duplicate]

不打扰是莪最后的温柔 提交于 2019-12-13 10:37:53
问题 This question already has an answer here : multiply multiple column and find sum of each column for multiple values (1 answer) Closed 6 years ago . this is my code here the paste function works for only two combination.i need the same code in a loop for more than two combinations at the same time. i<-2 while (i<=10) { results<-data.frame() results<- t(apply(data,1,function(x) combn(x,i,prod))) comb <- combn(colnames(data),i) colnames(results) <- apply(comb,i,function(x) paste(x[1],x[2])) i<-i

Passing variable name into sapply

浪尽此生 提交于 2019-12-13 04:06:43
问题 Here is a toy dataset: df1 <-data.frame(c("267119002","257051033",NA,"267098003","267099020","267047006")) names(df1)[1]<-"ID" df2 <-data.frame(c("257051033","267098003","267119002","267047006","267099020")) names(df2)[1]<-"ID" df2$vals <-c(11,22,33,44,55) and toy code: fetcher <-function(x){ y <- df2$vals[which(match(df2$ID,x)==TRUE)] return(y) } sapply(df1$ID,function(x) fetcher(x)) In the sapply statement, instead of using df1$ID , I need to use a variable name. As in: col <-"ID" sapply

Using grepl in R to match string

十年热恋 提交于 2019-12-12 09:49:18
问题 I have a frame data "testData" as follows: id content 1 I came from China 2 I came from America 3 I came from Canada 4 I came from Japan 5 I came from Mars And I also have another frame data "addr" as follows: id addr 1 America 2 Canada 3 China 4 Japan Then how can I use grepl , sapply or any other useful function in R to generate data into as follows: id content addr 1 I came from China China 2 I came from America America 3 I came from Canada Canada 4 I came from Japan Japan 5 I came from

R: Webscraping a list of URLs to get a DataFrame

心不动则不痛 提交于 2019-12-12 09:23:23
问题 I can see the correct data, but cannot put it on a Data Frame (It appears as a list of elements). I think the problem is my understanding of the apply family functions. Any hint is welcome. Here is a similar question, but I think it is better to post mine as it contains more details: Webscraping content across multiple pages using rvest package library(rvest) library(lubridate) library(dplyr) urls <- list("http://simple.ripley.com.pe/tv-y-video/televisores/ver-todo-tv", "http://simple.ripley

function over more than one list

妖精的绣舞 提交于 2019-12-12 05:46:54
问题 I do not use that many functions but when I do I tend to use an anon function and some form of apply . I now however am trying to write a function that works over items in a list. There are two lists that each have many items (by item I mean e.g. mylist1[1] ). All items are dataframes. I want to take the first dataframe from mylist1 and the first dataframe from mylist2 and run a bunch of functions over the columns in those dataframes. Then take the 2nd mylist1 item and the 2nd mylist2 item

creating variables using loop or apply in r

旧时模样 提交于 2019-12-12 03:33:04
问题 I'm trying to create a series of variables in R based on an ifelse function: comp1990<-ifelse(year_begin<1990 & year_end>1990,1,0) comp1991<-ifelse(year_begin<1991 & year_end>1991,1,0) comp1992<-ifelse(year_begin<1992 & year_end>1992,1,0) I'm doing this for years 1970-2007. Right now, I just have a line for every single year. In stata, I could do this in the following way: forvalues n=1970(1)2007 { gen comp\`n'== (year_begin<\`n' & year_end>\`n') } Is there a similarly straightforward way to

R : Triple summation over function dependent on three indicies

三世轮回 提交于 2019-12-12 03:30:56
问题 I am attempting to use R to to a triple summation over a function with three indices. I was easily able to do this in Mathematica with the following code: out = Sum[B[G[[k]]] * A[G[[k]], G[[j]], G[[i]]] * prod[G[[j]],G[[i]]], {k,1,Length[G]},{j,1,Length[G]},{i,1,Length[G]}] where G is a matrix, and B[.] , A[.] , and prod[.] are all predefined functions. In Mathematica, Sum[f,{k,k_min, k_max}, {j,j_min, j_max}, {i, i_min, i_max}] would evaluate the triple sum, Sum(k=k_min, k_max)[Sum(j=j_min,

subsetting a dataset in R [duplicate]

混江龙づ霸主 提交于 2019-12-11 19:28:21
问题 This question already has answers here : Filter group of rows based on sum of values from different column in R (2 answers) Closed 7 months ago . I have a question filtering a dataset based on sum of counts My file looks like this: g1 a 2 g1 a 3 g1 a 0 g1 b 1 g2 b 3 g2 c 4 g2 d 9 g3 e 1 g3 f 3 g4 g 10 g4 h 18 g4 i 23 First column is gene names. I want to calculate from the third column, the sum associated with each gene, for g1 it's 6 for g2 it's 16 and so on. Then the condition is if the sum