# R questions

« Prev
Topic
» Next
Topic
<123>
 0 votes Another simple question. I have right.a right.b and right.c as Chris suggested in his code. I would like to replace it by right[1] right[2] right[3] so later I could have i as a variable and write right[i]. Is there a way to do it with R? note that I cannot do it by a simple matrix because right.a has vectors of length 76038 right.b has vectors of lengths of length 71435 right.c has vectors of lengths of length 70942 #41 | Posted 5 years ago Posts 253 | Votes 4 Joined 5 Aug '10 | Email User
 0 votes A list is the thing that comes to mind - you could do: right.master <- list(right.a, right.b, right.c) Then there are several ways to access the info: I am listing but one > head(right.master[[1]])   MemberID ClaimsTruncated DaysInHospital   logdays AgeAtFirstClaim Sex 1 98324177               0              0 0.0000000           30-39   M 2 33899367               1              1 0.6931472             80+   F 3  5481382               0              1 0.6931472           20-29   F 4 69908334               0              0 0.0000000           60-69   F 5 29951458               0              0 0.0000000           40-49   M 6  8169687               0              0 0.0000000           60-69   F > head(right.master[[2]])   MemberID ClaimsTruncated DaysInHospital   logdays AgeAtFirstClaim Sex 1 90963501               0              0 0.0000000           70-79   F 2 85160905               0              0 0.0000000             0-9   M 3 45496648               0              0 0.0000000           30-39   F 4 45162557               0              0 0.0000000           20-29   F 5 68793937               0              0 0.0000000           30-39   M 6 25044772               0              1 0.6931472             80+   F > head(right.master[[3]])   MemberID ClaimsTruncated DaysInHospital logdays AgeAtFirstClaim Sex 1 20820036               0             NA      NA           70-79   M 2 14625274               1             NA      NA           70-79     3 99227820               0             NA      NA           10-19   M 4 74486714               0             NA      NA           70-79   F 5 92341995               0             NA      NA           40-49   F6  7127539               0             NA      NA           70-79    You can get variables similar to  the other way you can with data frames: for example: summary(right.master[[1]]$MemberID) If all else fails - you can always extract the individual list elements back to the data.frame: > right.aa > identical(right.a, right.aa) [1] TRUE  #42 | Posted 5 years ago | Edited 5 years ago Competition 20th Posts 194 | Votes 92 Joined 9 Jul '10 | Email User  0 votes thanks for the reply but unfortunately list is not good enough I thought right.master[[1]] is equivalent to right.a but it seems that it is not equivalent. and if I change right.a and add right.a$new_var then right.master[[1]] does not know about the new variable and I get an error if I type right.master[[1]]\$new_var It seems that by list the computer generates a new object that is not identical to the old object and it also mean waste of memory by the computer. I do not want the computer to use space for another object and I simply want the computer to understand that if I write right.master[i] I mean right.a(if i=1) or right.b(if i=2) or right.c(if i=3) #43 | Posted 5 years ago Posts 253 | Votes 4 Joined 5 Aug '10 | Email User
 0 votes Just came across this which may be of help. #44 | Posted 5 years ago Competition 1st | Overall 638th Posts 378 | Votes 178 Joined 22 Jun '10 | Email User
 0 votes how to remove "+" from a string with R? Unfortunately the following code (that help to remove spaces does not work and for some reason R ignore the last command(not that if I try to replace the + by the word plus I get strange results(namely "plus5plus+plus"). cleanstring<-function(y) + { + y<-gsub(" ","",y) + y<-gsub("-","to",y) + y<-gsub("+", "",y) + } y<-"5+" y<-cleanstring(y) y [1] "5+" #45 | Posted 5 years ago Posts 253 | Votes 4 Joined 5 Aug '10 | Email User
 0 votes I think that I solved the problem It seems that I need to have y<-gsub("+", "",y,,,TRUE) In order to replace correctly. I do not understand the logic behind the default definition. #46 | Posted 5 years ago Posts 253 | Votes 4 Joined 5 Aug '10 | Email User
 0 votes from ?gsub character string containing a regular expression (or character string for fixed = TRUE) to be matched in the given character vector. so if you don't use fixed = TRUE you have to supply a regular expression (see ?regex). Also, is a very good forum to get quick R help - although it can be a bit prickly. #47 | Posted 5 years ago Competition 1st | Overall 638th Posts 378 | Votes 178 Joined 22 Jun '10 | Email User
 0 votes I do not understand the logic behind the default false for fixed. I think that the logical default option is simply to replace everything, What is the benefit of the option not to use  fixed = TRUE? #48 | Posted 5 years ago Posts 253 | Votes 4 Joined 5 Aug '10 | Email User
 0 votes Uri Blass wrote: I do not understand the logic behind the default false for fixed. I think that the logical default option is simply to replace everything, What is the benefit of the option not to use  fixed = TRUE? gsub uses regular expressions by default, which are very powerful for finding and replacing patterns of text.  To turn this behaivor off and treat the 'find' text literally, set fixed=FALSE. #49 | Posted 5 years ago Competition 9th | Overall 344th Posts 565 | Votes 572 Joined 2 Mar '11 | Email User
<123>

## Reply

Flagging notifies Kaggle that this message is spam, inappropriate, abusive, or violates rules. Do not use flagging to indicate you disagree with an opinion or to hide a post.