sapply

Conditionally convert strings to a specific numeric value

☆樱花仙子☆ 提交于 2019-12-11 01:20:29
问题 I'm sure there is an easy answer for this , but I have scanned stack overflow and haven't been able to find a solution. It would seem that potentially a combination of sapply and ifelse functions would do the job (but I'm not sure). So I have a dataframe with characters, except one column which is a numeric value. ####Create dataframe which needs converting df <- data.frame(Sample_1 = rep(letters[1:3], each = 3), Sample_2 = rep("a", times = 9)) df$Number <- rep(seq(from=1,to=3,by=1)) I would

Trying to avoid for loop with sapply (for gsub)

本秂侑毒 提交于 2019-12-10 21:22:14
问题 Trying to avoid using a for loop in the following code by utilizing sapply , if at all possible. The solution with loop works perfectly fine for me, I'm just trying to learn more R and explore as many methods as possible. Objective: have a vector i and two vectors sf (search for) and rp (replace). For each i need to loop over sf and replace with rp where match. i = c("1 6 5 4","7 4 3 1") sf = c("1","2","3") rp = c("one","two","three") funn <- function(i) { for (j in seq_along(sf)) i = gsub(sf

R - A loop comparing elements in common between two hierarchical lists

做~自己de王妃 提交于 2019-12-10 19:30:06
问题 I have been trying, for some time, to build a matrix populated by the counts of elements in common between two herarchical lists. Here is some dummy data: site<-c('A','A','A','A','A','A','A','A','A','B','B','B','B','B','B') group<-c('A1','A1','A2','A2','A2','A3','A3','A3','A3', 'B1','B1','B2','B2','B2','B2') element<-c("red","orange","blue","black","white", "black","cream","yellow","purple","red","orange","blue","white","gray","salmon") d<-cbind(site,group,element) I created a list structure,

using sapply in R for ploting side by side graph

帅比萌擦擦* 提交于 2019-12-10 19:17:26
问题 I have the following code: dat <- read.table(text="Topic Project C10 C14 C03 C11 C16 C08 T1 P1 0.24 0.00 0.00 0.04 0.04 0.00 T2 P1 0.00 0.30 0.00 0.00 0.00 0.00 T3 P1 0.04 0.04 0.00 0.24 0.00 0.00 T4 P1 0.00 0.00 0.00 0.04 0.33 0.04 T5 P1 0.00 0.09 0.21 0.00 0.00 0.00 T6 P1 0.00 0.09 0.00 0.00 0.00 0.34 T1 P2 0.20 0.00 0.00 0.04 0.00 0.04 T2 P2 0.00 0.22 0.04 0.00 0.00 0.00 T3 P2 0.04 0.00 0.00 0.24 0.00 0.00 T4 P2 0.00 0.00 0.04 0.00 0.33 0.00 T5 P2 0.04 0.00 0.21 0.00 0.00 0.00 T6 P2 0.00 0

Ignore NA's in sapply function

怎甘沉沦 提交于 2019-12-10 15:09:22
问题 I am using R and have searched around for an answer but while I have seen similar questions, it has not worked for my specific problem. In my data set I am trying to use the NA 's as placeholders because I am going to return to them once I get part of my analysis done so therefore, I would like to be able to do all my calculations as if the NA 's weren't really there. Here's my issue with an example data table ROCA = c(1,3,6,2,1,NA,2,NA,1,NA,4,NA) ROCA <- data.frame (ROCA=ROCA) # converting

Determining if one value occurs once in a row of columns, but a second value doesn't occur at all

廉价感情. 提交于 2019-12-10 09:23:39
问题 Probably a terrible title, but I have a table of qualifiers stored as "1", "2", and "3". What I'm trying to do is is look in each row (approximately 300,000 rows, but variable.) and determine where a single "3" occurs, (if it occurs more than once, I am not interested in it) and the rest of the columns in that row have a "1", and return that to a list. (The number of columns and column names change based on the input files.) Instinctively I want to attempt this by doing nested for loops that

R: Apply function on specific columns preserving the rest of the dataframe

微笑、不失礼 提交于 2019-12-09 09:23:31
问题 I'd like to learn how to apply functions on specific columns of my dataframe without "excluding" the other columns from my df. For example i'd like to multiply some specific columns by 1000 and leave the other ones as they are. Using the sapply function for example like this: a<-as.data.frame(sapply(table.xy[,1], function(x){x*1000})) I get new dataframes with the first column multiplied by 1000 but without the other columns that I didn't use in the operation. So my attempt was to do it like

sapply vs. lapply while reading files and rbind'ing them

痞子三分冷 提交于 2019-12-08 19:44:27
I followed Hadley's thread: Issue in Loading multiple .csv files into single dataframe in R using rbind to read multiple CSV files and then convert them to one dataframe. I also experimented with lapply vs. sapply as discussed on Grouping functions (tapply, by, aggregate) and the *apply family . Here's my first CSV file: dput(File1) structure(list(First.Name = structure(c(1L, 2L, 1L, 1L, 1L), .Label = c("A", "C"), class = "factor"), Last.Name = structure(c(1L, 2L, 2L, 2L, 2L), .Label = c("B", "D"), class = "factor"), Income = c(55L, 23L, 34L, 45L, 44L), Tax = c(23L, 21L, 22L, 24L, 25L),

Using variations of `apply` in R

不想你离开。 提交于 2019-12-08 11:18:38
问题 Often times in research we have to do a summary table. I would like to create a table using tapply in R. The only problem is I have 40 variables and I would like to basically perform the same operation for all 40 variables. Here is an example of the data Age Wt Ht Type 79 134 66 C 67 199 64 C 39 135 78 T 92 149 61 C 33 138 75 T 68 139 71 C 95 198 62 T 65 132 65 T 56 138 81 C 71 193 78 T Essentially I would like to get it to produce the means of all the variables given the Type . It should

Apply List of functions on List of columns based on different combinations

别等时光非礼了梦想. 提交于 2019-12-08 07:51:04
问题 I have a dataframe df with three categorical variables cat1 , cat2 , cat3 and two continuous variables con1 , con2 . I would like to compute list of functions sd , mean on list of columns con1 , con2 based on different combinations of list of columns cat1 , cat2 , cat3 . I have done them explicitly subsetting all different combinations. # Random generation of values for categorical data set.seed(33) df <- data.frame(cat1 = sample( LETTERS[1:2], 100, replace=TRUE ), cat2 = sample( LETTERS[3:5]