### Spojovani retezcu paste("The value of pi:", pi) paste("I", "love", "R", sep = "-") paste("X", 1:5, sep = ".") paste(1:3, c("!", "?", "+"), sep = "") paste(1:3, c("!", "?", "+"), sep = "", collapse = "") cat(my_string) cat(my_string, "with R") cat(my_string, "with R", sep = " =) ") cat(1:10, sep = "-") cat(month.name[1:4], sep = " ") cat("Loooooooooong strings", "can be displayed", "in a nice format","by using the 'fill' argument") cat(my_string, "with R", file = "output.txt") ##### my_string = "programming with data is fun" print(my_string) print(my_string, quote = FALSE) noquote(my_string) no_quotes = noquote(c("some", "quoted", "text", "!%^(&=")) no_quotes no_quotes[2:3] ##### Format retezce ####### format(13.7) format(13.12345678) format(13.7, nsmall = 3) format(c(6, 13.1), digits = 2) format(c(6, 13.1), digits = 2, nsmall = 1) format(1/1:5, digits = 2) format(format(1/1:5, digits = 2), width = 6, justify = "c") format(c("A", "BB", "CCC"), width = 5, justify = "centre") format(c("A", "BB", "CCC"), width = 5, justify = "left") format(c("A", "BB", "CCC"), width = 5, justify = "right") format(c("A", "BB", "CCC"), width = 5, justify = "none") format(123456789, big.mark = ",") # '%f' indicates 'fixed point' decimal notation sprintf("%f", pi) # decimal notation with 3 decimal digits sprintf("%.3f", pi) # 1 integer and 0 decimal digits sprintf("%1.0f", pi) # decimal notation with 3 decimal digits sprintf("%5.1f", pi) sprintf("%05.1f", pi) # print with sign (positive) sprintf("%+f", pi) # prefix a space sprintf("% f", pi) # left adjustment sprintf("%-10f", pi) # exponential decimal notation 'e' sprintf("%e", pi) # exponential decimal notation 'E' sprintf("%E", pi) # number of significant digits (6 by default) sprintf("%g", pi) ##################### toString(17.04) toString(c(17.04, 1978)) toString(c("Bonjour", 123, TRUE, NA, log(exp(1)))) toString(c("one", "two", "3333333333"), width = 8) toString(c("one", "two", "3333333333"), width = 12) ##################### length("How many characters?") length(c("How", "many", "characters?")) nchar("How many characters?") nchar(c("How", "many", "characters?")) # to lower case tolower(c("aLL ChaRacterS in LoweR caSe", "ABCDE")) casefold("aLL ChaRacterS in LoweR caSe") # to upper case toupper(c("All ChaRacterS in Upper Case", "abcde")) casefold("All ChaRacterS in Upper Case", upper = TRUE) # replace 'a' by 'A' chartr("a", "A", "This is a boring string") chartr("ai", "X", "This is a bad example") crazy = c("Here's to the crazy ones", "The misfits", "The rebels") chartr("aei", "#!?", crazy) # extract 'bcd' substr("abcdef", start=2, stop=4) # replace 2nd letter with hash symbol x = c("may", "the", "force", "be", "with", "you") substr(x, 2, 2) <- "#" x # replace 2nd and 3rd letters with happy face y = c("may", "the", "force", "be", "with", "you") substr(y, 2, 3) <- ":)" y # replacement with recycling z = c("may", "the", "force", "be", "with", "you") substr(z, 2, 3) <- c("#", "@") z substring("ABCDEF", 2, 4) # extract each letter substring("ABCDEF", 1:6, 1:6) # multiple replacement with recycling text = c("more", "emotions", "are", "better", "than", "less") substring(text, 1:3) <- c(" ", "zzz") text set11 = c("today", "produced", "example", "beautiful", "a", "nicely") # sort (decreasing order) sort(set11) # sort (increasing order) sort(set11, decreasing = TRUE) ################################################### str = "Splitting sentence into words" strsplit(str, " ") unlist(strsplit(str, " ")) a <- "Alabama-Alaska-Arizona-Arkansas-California" strsplit(a, split = "-") ####################################################### myStrings <- paste(1:3, month.name, sep = ". ") myStrings # Is a pattern present (returns a logical vector)? grepl("ember", myStrings) # In which elements is a pattern present (returns indices)? grep("ember", myStrings) # In which elements is a pattern present (returns the values)? grep("ember", myStrings, value = TRUE) ####################################################### startsWith(month.name, "J") endsWith(month.name, "ember") ####################################################### substr(month.name, 1, 3) substring(month.name, 1, 3) strtrim(month.name, 3) trimws(" This has trailing spaces. ") ####################################################### library(stringr) str_c("May", "The", "Force", "Be", "With", "You") # removing zero length objects paste("May", "The", "Force", NULL, "Be", "With", "You", character(0)) str_c("May", "The", "Force", NULL, "Be", "With", "You", character(0)) str_c("May", "The", "Force", "Be", "With", "You", sep = "_") str_join("May", "The", "Force", "Be", "With", "You", sep = "-") # analogicka fce k predchozi some_text = c("one", "two", "three", NA, "five") nchar(some_text) str_length(some_text) # some factor some_factor = factor(c(1, 1, 1, 2, 2, 2), labels = c("good", "bad")) some_factor nchar(some_factor) str_length(some_factor) lorem = "Lorem Ipsum" substring(lorem, first = 1, last = 5) str_sub(lorem, start = 1, end = 5) str_sub("adios", 1:3) resto = c("brasserie", "bistrot", "creperie", "bouchon") substring(resto, first = -4, last = -1) str_sub(resto, start = -4, end = -1) str_sub(lorem, seq_len(nchar(lorem))) substring(lorem, seq_len(nchar(lorem))) str_sub(lorem, -seq_len(nchar(lorem))) substring(lorem, -seq_len(nchar(lorem))) lorem = "Lorem Ipsum" str_sub(lorem, 1, 5) <- "Nullam" lorem lorem = "Lorem Ipsum" str_sub(lorem, -1) <- "Nullam" lorem lorem = "Lorem Ipsum" str_sub(lorem, c(1, 7), c(5, 8)) <- c("Nullam", "Enim") lorem ################## str_dup("hola", 3) str_dup("adios", 1:3) words = c("lorem", "ipsum", "dolor", "sit", "amet") str_dup(words, 2) str_dup(words, 1:5) ################## str_pad("hola", width = 7) str_pad("adios", width = 7, side = "both") str_pad("hashtag", width = 8, pad = "#") str_pad("hashtag", width = 9, side = "both", pad = "-") ################### bad_text = c("This", " example ", "has several ", " whitespaces ") str_trim(bad_text, side = "left") str_trim(bad_text, side = "right") str_trim(bad_text, side = "both") ################### change = c("Be the change", "you want to be") # extract first word word(change, 1) # extract second word word(change, 2) # extract last word word(change, -1) # extract all but the first words word(change, 2, -1) ##################### library(stringi) stri_reverse("dna") library(Biostrings) str_rev("dna")