1

i want to make a function where, if an argument includes "A", "T", "G", "C" n numbers of times and it could replace T into U and gives back all the changed argument, then what should be the appropriate way to code this in R? set of code which i have made or i want to use for y personal preference is=

    ``` r
data=c("A","T","G","C")

change<-function(compound)
+ {
+     molecules<-unlist(strsplit(compound,""))
+     
+     
+     gsub("T","U",string)
+ }
change("ATGC")
Error in gsub("T", "U", string) : object 'string' not found
#> Error: <text>:9:3: unexpected '}'
#> 8: +     gsub("T","U",string)
#> 9: + }
#>      ^
```

Created on 2018-05-22 by the reprex package (v0.2.0).

Argument would be like change("AAAATTTTGGGGCCCCC") and it should give an output=AAAAUUUUGGGGCCCC

Please help me with this problem. Thank you

  • When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick May 22 '18 at 17:52
  • 3
    `gsub("T", "U", string)` ? – G5W May 22 '18 at 17:53
  • Or `chartr("T", "U", x)` – Jaap May 22 '18 at 17:58
  • ``` r data=c("A","T","G","C") change<-function(compound) + { + molecules<-unlist(strsplit(compound,"")) + + + gsub("T","U",string) + } change("ATGC") Error in gsub("T", "U", string) : object 'string' not found Called from: gsub("T", "U", string) #> Error: :9:3: unexpected '}' #> 8: + gsub("T","U",string) #> 9: + } #> ^ ``` Created on 2018-05-22 by the [reprex package](http://reprex.tidyverse.org) (v0.2.0). it os showing me error with this. @G5W – Mayank Rajput May 22 '18 at 17:59
  • Please edit your original question. Comment section is not designed to handle code chunks. – Roman Luštrik May 22 '18 at 18:02
  • @Jaap thank you for this but it does not give me the same number of characters as my argument. also i want output in AUUUUGGCCC manner not in "A","U","G","C" manner – Mayank Rajput May 22 '18 at 18:03
  • Please check the output of `chartr("T", "U", "AAAATTTTGGGGCCCCC")` or `gsub("T", "U", "AAAATTTTGGGGCCCCC")`. Both give the desired output: `"AAAAUUUUGGGGCCCCC"`. – Jaap May 22 '18 at 18:07
  • @MayankRajput There might be an inconcsistency in your post. The more complete example says `change("AAAATTTTGGGGCCCCC")` (ie, there's a single element, consisting of multiple characters). The other example says `c("A","T","G","C")` (ie, multiple elements, consisting of a single character). – wibeasley May 22 '18 at 18:08
  • @Jaap what if i want to make a function for this, i dont want to use chartr or gsub directly – Mayank Rajput May 22 '18 at 18:12
  • 2
    Why would you like to make a new function when `gsub` and `chartr` are ready available? – Jaap May 22 '18 at 18:13
  • @wibeasley no sir i mean my argument could contain n number times A or T or G or C but it give us back exact number of arguments with the replacement of T with U – Mayank Rajput May 22 '18 at 18:14
  • @Jaap thank you sir i will try to accommodate this in the further steps, i'll ask here if i would stuck in future. – Mayank Rajput May 22 '18 at 18:16
  • @Jaap sir, what if i want to change more character for example A to U, T to A, G to C and C to G in a single step then what should i do. – Mayank Rajput May 22 '18 at 18:43
  • 3
    just adapt the `chartr`-example to `chartr("ATGC", "UACG", "AAAATTTTGGGGCCCCC")` – Jaap May 22 '18 at 18:45

0 Answers0