Re: [R] function which returns number of occurrences of a pattern in string
Bob and Max, I thank you. It helped me much. 2016-10-21 3:47 GMT+02:00 Bob Rudis : > `stringi::stri_count()` > > I know that the `stringr` pkg saves some typing (it wraps the > `stringi` pkg), but you should really just use the `stringi` package. > It has many more very useful functions with not too much more typing. > > On Thu, Oct 20, 2016 at 5:47 PM, Jan Kacaba wrote: > > Hello dear R-help > > > > I tried to find function which returns number of occurrences of a pattern > > in string. The closest match I've found is str_locate_all in stringr > > package. I can use str_locate_all but write my function but I don't want > > reinvent wheel. > > > > JK > > > > [[alternative HTML version deleted]] > > > > __ > > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide http://www.R-project.org/ > posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] function which returns number of occurrences of a pattern in string
Hello dear R-help I tried to find function which returns number of occurrences of a pattern in string. The closest match I've found is str_locate_all in stringr package. I can use str_locate_all but write my function but I don't want reinvent wheel. JK [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] strange output of cat function used in recursive function
2016-10-01 18:02 GMT+02:00 David Winsemius : > >> On Oct 1, 2016, at 8:44 AM, Jan Kacaba wrote: >> >> Hello Dear R-help >> >> I tried to understand how recursive programming works in R. Bellow is >> simple recursive function. >> >> binary1 <- function(n) { >> if(n > 1) { >>binary(as.integer(n/2)) >> } >> cat(n %% 2) >> } > > Did you mean to type "binary1(as.integer(n)"? Yes I meant that. >> When I call binary1(10) I get 1010. I believe that cat function stores >> value to a buffer appending values as recursion proceeds and at the >> end it prints the buffer. Am I right? > > No. Read the ?cat help page. It returns NULL. The material you see at the > console is a side-effect. >> >> I tried to modify the function to get some understanding: >> >> binary2 <- function(n) { >> if(n > 1) { >>binary2(as.integer(n/2)) >> } >> cat(n %% 2, sep=",") >> } >> >> With call binary2(10) I get also 1010. Why the output is not separated >> by commas? > > I think because there is nothing to separate when it prints (since there was > no "buffer". If I use function: binary3 <- function(n) { if(n > 1) { binary3(as.integer(n/2)) } cat(n %% 2, ",") } and call binary3(10) the console output is separated. So there must be some kind of buffer and also it looks like there is some inconsistency in how cat function behaves. Probably there is other explanation. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] strange output of cat function used in recursive function
Hello Dear R-help I tried to understand how recursive programming works in R. Bellow is simple recursive function. binary1 <- function(n) { if(n > 1) { binary(as.integer(n/2)) } cat(n %% 2) } When I call binary1(10) I get 1010. I believe that cat function stores value to a buffer appending values as recursion proceeds and at the end it prints the buffer. Am I right? I tried to modify the function to get some understanding: binary2 <- function(n) { if(n > 1) { binary2(as.integer(n/2)) } cat(n %% 2, sep=",") } With call binary2(10) I get also 1010. Why the output is not separated by commas? If I use in binary2 function cat(n %% 2, ",") on last line, the output is separated. Outside recursive function the cat function prints separated output in both cases e.g. cat(c(1:10), sep=",") and cat(c(1:10), ",") Derek __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] R Studio: Run script upon saving or exiting
Dear R help, I would like to run script upon saving project files or exiting the R Studio. For example I would like to backup whole project in another directory. The backup directory should be named such that incremental version number will added to project name. Is it somehow possible? Even better would be if someone can also quickly go through file versions. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] print all variables inside function
Hello dear R-help I would like to use some short and simple names multiple times inside one script without collisions. I need to wrap the variables inside some object. I know I can use class function or environment. For example as follows: exmp1<-function(){ # knowns pa=0.35 pb=0.35 pc=0.30 pad=0.015 pbd=0.010 pcd=0.020 # unknowns pd=pa*pad+pb*pbd+pc*pcd pdc=pc*pcd/pd pda=pa*pad/pd pba=pb*pbd/pd y<-c(pad=pad,pbd=pbd,pcd=pcd,pd=pd,pdc=pdc,pda=pda,pba=pba) # this line I would like to automate so I don't have to write it every time return(y) } output<-exmp1() Is it somehow possible to print 'Unknows' and 'Knowns' from exmp1 function without the need of explicitly write the 'y' line which puts all variables inside list? For example with an imaginary function 'fprint' which takes exmp1 as the input: fprint(exmp1). __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] break string at specified possitions
Excellent Hervé, thank you. 2016-05-13 11:48 GMT+02:00 Hervé Pagès : > Hi, > > Here is the Biostrings solution in case you need to chop a long > string into hundreds or thousands of fragments (a situation where > base::substring() is very inefficient): > > library(Biostrings) > > ## Call as.character() on the result if you want it back as > ## a character vector. > fast_chop_string <- function(x, ends) > { > if (!is(x, "XString")) > x <- as(x, "XString") > extractAt(x, at=PartitioningByEnd(ends)) > } > > Will be much faster than substring (e.g. 100x or 1000x) when > chopping a string like a Human chromosome into hundreds or > thousands of fragments. > > Biostrings is a Bioconductor package: > > https://bioconductor.org/packages/Biostrings > > Cheers, > H. > > > > On 05/12/2016 01:18 AM, Jan Kacaba wrote: >> >> Nice solution Jim, thank you. >> >> >> >> 2016-05-12 2:45 GMT+02:00 Jim Lemon : >>> >>> Hi again, >>> Sorry, that should be: >>> >>> chop_string<-function(x,ends) { >>> starts<-c(1,ends[-length(ends)]+1) >>> return(substring(x,starts,ends)) >>> } >>> >>> Jim >>> >>> On Thu, May 12, 2016 at 10:05 AM, Jim Lemon wrote: >>>> >>>> Hi Jan, >>>> This might be helpful: >>>> >>>> chop_string<-function(x,ends) { >>>> starts<-c(1,ends[-length(ends)]-1) >>>> return(substring(x,starts,ends)) >>>> } >>>> >>>> Jim >>>> >>>> >>>> On Thu, May 12, 2016 at 7:23 AM, Jan Kacaba >>>> wrote: >>>>> >>>>> Here is my attempt at function which computes margins from positions. >>>>> >>>>> require("stringr") >>>>> require("dplyr") >>>>> >>>>> ends<-seq(10,100,8) # end margins >>>>> test_string<-"Lorem ipsum dolor sit amet, consectetuer adipiscing >>>>> elit. Aliquam in lorem sit amet leo accumsan lacinia." >>>>> >>>>> sekoj=function(ends){ >>>>>l_ends<-length(ends) >>>>>begs=vector(mode="integer",l_ends) >>>>>begs[1]=1 >>>>>for (i in 2:(l_ends)){ >>>>> begs[i]<-ends[i-1]+1 >>>>>} >>>>>margs<-rbind(begs,ends) >>>>>margs<-cbind(margs,c(ends[l_ends]+1,-1)) >>>>>#rownames(margs)<-c("beg","end") >>>>>return(margs) >>>>> } >>>>> margins<-sekoj(ends) >>>>> str_sub(test_string,margins[1,],margins[2,]) %>% print >>>>> >>>>> Code to run in browser: >>>>> http://www.r-fiddle.org/#/fiddle?id=rVmNVxDV >>>>> >>>>> 2016-05-11 23:12 GMT+02:00 Bert Gunter : >>>>>> >>>>>> Dunno -- but you might have a look at Hadley Wickham's 'stringr' >>>>>> package: >>>>>> https://cran.r-project.org/web/packages/stringr/stringr.pdf >>>>>> >>>>>> Cheers, >>>>>> >>>>>> Bert >>>>>> >>>>>> >>>>>> Bert Gunter >>>>>> >>>>>> "The trouble with having an open mind is that people keep coming along >>>>>> and sticking things into it." >>>>>> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) >>>>>> >>>>>> >>>>>> On Wed, May 11, 2016 at 1:12 PM, Jan Kacaba >>>>>> wrote: >>>>>>> >>>>>>> Dear R-help >>>>>>> >>>>>>> I would like to split long string at specified precomputed positions. >>>>>>> 'substring' needs beginings and ends. Is there a native function >>>>>>> which >>>>>>> accepts positions so I don't have to count second argument? >>>>>>> >>>>>>> For example I have vector of possitions pos<-c(5,10,19). Substring >>>>>>> needs input first=c(1,6,11) and last=c(5,10,19). There is no problem >>>>>>> to write my own function. Just asking. >>>>>>> >>>>>>> Derek >>>>>>> >>>>>>> __ >>>>>>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >>>>>>> https://stat.ethz.ch/mailman/listinfo/r-help >>>>>>> PLEASE do read the posting guide >>>>>>> http://www.R-project.org/posting-guide.html >>>>>>> and provide commented, minimal, self-contained, reproducible code. >>>>> >>>>> >>>>> __ >>>>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >>>>> https://stat.ethz.ch/mailman/listinfo/r-help >>>>> PLEASE do read the posting guide >>>>> http://www.R-project.org/posting-guide.html >>>>> and provide commented, minimal, self-contained, reproducible code. >> >> >> __ >> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> > > -- > Hervé Pagès > > Program in Computational Biology > Division of Public Health Sciences > Fred Hutchinson Cancer Research Center > 1100 Fairview Ave. N, M1-B514 > P.O. Box 19024 > Seattle, WA 98109-1024 > > E-mail: hpa...@fredhutch.org > Phone: (206) 667-5791 > Fax:(206) 667-1319 __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] break string at specified possitions
Nice solution Jim, thank you. 2016-05-12 2:45 GMT+02:00 Jim Lemon : > Hi again, > Sorry, that should be: > > chop_string<-function(x,ends) { > starts<-c(1,ends[-length(ends)]+1) > return(substring(x,starts,ends)) > } > > Jim > > On Thu, May 12, 2016 at 10:05 AM, Jim Lemon wrote: >> Hi Jan, >> This might be helpful: >> >> chop_string<-function(x,ends) { >> starts<-c(1,ends[-length(ends)]-1) >> return(substring(x,starts,ends)) >> } >> >> Jim >> >> >> On Thu, May 12, 2016 at 7:23 AM, Jan Kacaba wrote: >>> Here is my attempt at function which computes margins from positions. >>> >>> require("stringr") >>> require("dplyr") >>> >>> ends<-seq(10,100,8) # end margins >>> test_string<-"Lorem ipsum dolor sit amet, consectetuer adipiscing >>> elit. Aliquam in lorem sit amet leo accumsan lacinia." >>> >>> sekoj=function(ends){ >>> l_ends<-length(ends) >>> begs=vector(mode="integer",l_ends) >>> begs[1]=1 >>> for (i in 2:(l_ends)){ >>> begs[i]<-ends[i-1]+1 >>> } >>> margs<-rbind(begs,ends) >>> margs<-cbind(margs,c(ends[l_ends]+1,-1)) >>> #rownames(margs)<-c("beg","end") >>> return(margs) >>> } >>> margins<-sekoj(ends) >>> str_sub(test_string,margins[1,],margins[2,]) %>% print >>> >>> Code to run in browser: >>> http://www.r-fiddle.org/#/fiddle?id=rVmNVxDV >>> >>> 2016-05-11 23:12 GMT+02:00 Bert Gunter : >>>> Dunno -- but you might have a look at Hadley Wickham's 'stringr' package: >>>> https://cran.r-project.org/web/packages/stringr/stringr.pdf >>>> >>>> Cheers, >>>> >>>> Bert >>>> >>>> >>>> Bert Gunter >>>> >>>> "The trouble with having an open mind is that people keep coming along >>>> and sticking things into it." >>>> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) >>>> >>>> >>>> On Wed, May 11, 2016 at 1:12 PM, Jan Kacaba wrote: >>>>> Dear R-help >>>>> >>>>> I would like to split long string at specified precomputed positions. >>>>> 'substring' needs beginings and ends. Is there a native function which >>>>> accepts positions so I don't have to count second argument? >>>>> >>>>> For example I have vector of possitions pos<-c(5,10,19). Substring >>>>> needs input first=c(1,6,11) and last=c(5,10,19). There is no problem >>>>> to write my own function. Just asking. >>>>> >>>>> Derek >>>>> >>>>> __ >>>>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >>>>> https://stat.ethz.ch/mailman/listinfo/r-help >>>>> PLEASE do read the posting guide >>>>> http://www.R-project.org/posting-guide.html >>>>> and provide commented, minimal, self-contained, reproducible code. >>> >>> __ >>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >>> https://stat.ethz.ch/mailman/listinfo/r-help >>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >>> and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] break string at specified possitions
Here is my attempt at function which computes margins from positions. require("stringr") require("dplyr") ends<-seq(10,100,8) # end margins test_string<-"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam in lorem sit amet leo accumsan lacinia." sekoj=function(ends){ l_ends<-length(ends) begs=vector(mode="integer",l_ends) begs[1]=1 for (i in 2:(l_ends)){ begs[i]<-ends[i-1]+1 } margs<-rbind(begs,ends) margs<-cbind(margs,c(ends[l_ends]+1,-1)) #rownames(margs)<-c("beg","end") return(margs) } margins<-sekoj(ends) str_sub(test_string,margins[1,],margins[2,]) %>% print Code to run in browser: http://www.r-fiddle.org/#/fiddle?id=rVmNVxDV 2016-05-11 23:12 GMT+02:00 Bert Gunter : > Dunno -- but you might have a look at Hadley Wickham's 'stringr' package: > https://cran.r-project.org/web/packages/stringr/stringr.pdf > > Cheers, > > Bert > > > Bert Gunter > > "The trouble with having an open mind is that people keep coming along > and sticking things into it." > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) > > > On Wed, May 11, 2016 at 1:12 PM, Jan Kacaba wrote: >> Dear R-help >> >> I would like to split long string at specified precomputed positions. >> 'substring' needs beginings and ends. Is there a native function which >> accepts positions so I don't have to count second argument? >> >> For example I have vector of possitions pos<-c(5,10,19). Substring >> needs input first=c(1,6,11) and last=c(5,10,19). There is no problem >> to write my own function. Just asking. >> >> Derek >> >> __ >> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] break string at specified possitions
Dear R-help I would like to split long string at specified precomputed positions. 'substring' needs beginings and ends. Is there a native function which accepts positions so I don't have to count second argument? For example I have vector of possitions pos<-c(5,10,19). Substring needs input first=c(1,6,11) and last=c(5,10,19). There is no problem to write my own function. Just asking. Derek __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] row names, coulmn names
Hello dear R helpers, Is it possible to have more than 1 row for column names in data.frame, array, tbl_df? I would like to have column numbers in the first row, string names in the second row, physical unit in third row. How would I do it? Derek [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] inserting row(column) in array or dataframe at specified row(column)
Hello dear R users, Is there a function or package which can insert row, column or array in another array at specified place (row or column)? I have made several attempts at this function optimizing both speed, code readability and ease of use. The functions are of following format: appcol=function(original_array, inserted_object, column_number, overwrite=FALSE) # If overwrite=TRUE the columns after column_number are ovewritten by inserted_object else the columns after column_number are shifted. Now I have started using package dplyr and it seams that there is no inserting function either. One can only append at the end or at the beginning of tbl_df. Is it true? [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] (windows) opening document with particular exe file
Hello dear R, I dont have specific task on mind just learning R. 1) Is it possible to open a document for example path1\myfile.pdf with program path2\pdfviewer.exe ? How would I do it in win? Does it differ in linux? 2) Is it possible to run a program and supply to it some streams? The streams are for example txt file or web address. One specific task which comes to mid: I would like to draw in inkscape programmatically with script. Is it somehow possible? Thank you very much for any help in advance. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Accented characters, windows
Duncun, thank you for your reply. My encoding is: > Sys.getlocale('LC_CTYPE') [1] "Czech_Czech Republic.1250" In RStudio I use UTF-8. I tried also other recommended encodings but some characters are still misrepresented. I've found solution to this. To correctly display strings in RStudio I have to convert strings: iconv(x,"CP1250","UTF-8") If I want to write string into file: zz=file("myfile.txt", "w", encoding="UTF-8") cat(x,file = zz, sep = "\n") It seems there is no need using icon() if I just need to write string to a file. I hope there is no problem processing strings with other functions like paste, strsplit, grep though. Derek 2016-03-30 0:56 GMT+02:00 Duncan Murdoch : > On 29/03/2016 5:39 PM, Jan Kacaba wrote: > >> I have problem with accented characters. My OS is Win 8.1 and I'm using >> RStudio. >> >> I make string : >> av="ěščřž" >> >> When I call "av" I get result bellow. >> >>> av >>> >> [1] "ìšèøž" >> >> The resulting characters are different. I have similar problem when I >> write >> string to a file. In RGUI if I call "av" it prints characters correctly, >> but using "write" function to print string in a file results in the same >> problem. >> >> Can you please help me how to deal with it? >> > > You don't say what code page you're using. > > R in Windows has a long standing problem that it works mainly in the local > code page, rather than working in UTF-8 as most other systems do. (This is > due to the fact that when the internationalization was put in, UTF-8 was > exotic, rather than ubiquitous as it is now.) So R can store UTF-8 strings > on any system, but for display it converts them to the local code page, and > that conversion can lose information if the characters aren't supported > locally. > > With your string, I don't see the same thing as you, I see > > "ešcrž" > > which is also incorrect, but looks a little closer, because it does a > better approximation in my code page. > > So if you think my result is better than yours, you could change your > system to code page 437 as I'm using, but that will probably cause you > worse problems. > > Probably the only short term solution that would be satisfactory is to > stop using Windows. At some point in the future the internal character > handling in R needs an overhaul, but that's a really big, really thankless > job. Perhaps Microsoft/Revolution will donate some programmer time to do > it, but more likely, it will wait for volunteers in R Core to do it. I > don't think it will happen in 2016. > > Duncan Murdoch > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Accented characters, windows
I have problem with accented characters. My OS is Win 8.1 and I'm using RStudio. I make string : av="ěščřž" When I call "av" I get result bellow. > av [1] "ìšèøž" The resulting characters are different. I have similar problem when I write string to a file. In RGUI if I call "av" it prints characters correctly, but using "write" function to print string in a file results in the same problem. Can you please help me how to deal with it? [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] R studio kniter
Hello, is it possible to run kiniter by script instead by clicking on button compile PDF? Say I have "texfile.rnw" and "myscript.R". I would like to knit texfile.rnw by runnig script "myscript.R". In "myscript.R" I would write something like this: knit("texfile.rnw") [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] treating integer(0) and NULL in conditions and loops
Hello, I have following problem in loops. It occurred to me multiple times bellow is an example. Inside if() I have sometimes function f(x) which may return integer(0). If I test f(x)>1 and f(x)=integer(0) I get error. Maybe it can be solved more eloquently without loop or swithces. I don't know. Example: a=c("ab","abc","abcd","abcde","abcdefghjk") # vector from which new strings will be constructed svec=NULL # vector of string rz=NULL # string for (i in 1:10){ if (nchar(rz)>6){ svec[i]=rz rz=NULL } if (nchar(a[i])+nchar(rz))<6){ rz=paste(rz,a[i]) } if (nchar(rz)+nchar(a[i+1]>6){ svec[i]=rz rz=NULL } } I'm not interested how to treat nchar() function in particular but general function. One solution which comes to mind is to redefine function for example nchar() function like this: new.nchar=function(x){ if (length(nchar(rz))==0){z=0} if (length(nchar(rz))>0){z=nchar(rz)} return(z) } Is it correct way of doing this or is there a better way without the need of redefining new function? [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] assign a vector to list sequence
Hello I would like to assign a vector to list sequence. I'm trying my code bellow, but the output is not what inteded. # my code mls=vector(mode="list") # my list cseq=c(1:3) # my vector mls[cseq]=cseq I get following: [[1]] [1] 1 [[1]] [2] 2 [[1]] [2] 3 What I need is this: [[1]] [1] 1 2 3 [[1]] [2] 1 2 3 [[1]] [2] 1 2 3 [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] trying to reach R-help
Hello, I'm rather desperate so please excuse me if I'm using wrong emails. I'm sad that the forum at nabble is not functinal as before. I'm by no way want to critique something. I just don't understand in which way is mailing better than forum. I'm subscriber to R-help, but it seems that I'm unabble to reach R-help. I have sent some emails to r-help@r-project.org, but I get no response. My new posts aren't listed at http://r.789695.n4.nabble.com/R-help-f789696.html anymore either. Can you please tell me if I do something wrong or how should I reach R-help? Will my post be listed at nabble if reach R-help correctly? Thank you very much for answer. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] missing values in csv file
In my original data a csv file I have missing values. If I use read.table the missing values are replaced by NAs. Is it possible to get object where missing values aren't replaced with NAs? Is it possible to replace NAs with empty space? [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.