diff options
| author | wanderdoc <wanderdoc@googlemail.com> | 2019-12-29 21:04:33 +0100 |
|---|---|---|
| committer | wanderdoc <wanderdoc@googlemail.com> | 2019-12-29 21:04:33 +0100 |
| commit | 2ee588dad3c8863f75ab32bcde28bd8d50eb9fd4 (patch) | |
| tree | b524b682530d82ad9d1afae7d1228ccfefeaf044 /challenge-040/wanderdoc/R | |
| parent | 6a64d6f60114dd9be9cc1c820eaf7c729f20b1c7 (diff) | |
| download | perlweeklychallenge-club-2ee588dad3c8863f75ab32bcde28bd8d50eb9fd4.tar.gz perlweeklychallenge-club-2ee588dad3c8863f75ab32bcde28bd8d50eb9fd4.tar.bz2 perlweeklychallenge-club-2ee588dad3c8863f75ab32bcde28bd8d50eb9fd4.zip | |
Just tried the same in R.
Diffstat (limited to 'challenge-040/wanderdoc/R')
| -rw-r--r-- | challenge-040/wanderdoc/R/ch-1.R | 20 | ||||
| -rw-r--r-- | challenge-040/wanderdoc/R/ch-2.R | 20 |
2 files changed, 40 insertions, 0 deletions
diff --git a/challenge-040/wanderdoc/R/ch-1.R b/challenge-040/wanderdoc/R/ch-1.R new file mode 100644 index 0000000000..dc13f7537a --- /dev/null +++ b/challenge-040/wanderdoc/R/ch-1.R @@ -0,0 +1,20 @@ +# TASK #1 +# Show multiple arrays content. +# You are given two or more arrays. Write a script to display values of each list at a given index. + +# Help function qw: +# https://stackoverflow.com/questions/520810/does-r-have-quote-like-operators-like-perls-qw +qw <- function(x) unlist(strsplit(x, "[[:space:]]+")) # flodel in this thread. + +as1<-qw("I L O V E Y O U") +as2<-qw("2 4 0 3 2 0 1 9") +as3<-qw("! ? £ $ % ^ & *") + + +dfs<-rbind(as1, as2, as3) + + +for (col in 1:ncol(dfs)) +{ + cat(dfs[,col], "\n") +} diff --git a/challenge-040/wanderdoc/R/ch-2.R b/challenge-040/wanderdoc/R/ch-2.R new file mode 100644 index 0000000000..f4520d298f --- /dev/null +++ b/challenge-040/wanderdoc/R/ch-2.R @@ -0,0 +1,20 @@ +# TASK #2 +# Sort SubList +# You are given a list of numbers and set of indices belong to the list. Write a script to sort the values belongs to the indices. +# +# For example, +# List: [ 10, 4, 1, 8, 12, 3 ] +# Indices: 0,2,5 +# +# We would sort the values at indices 0, 2 and 5 i.e. 10, 1 and 3. +# +# Final List would look like below: +# List: [ 1, 4, 3, 8, 12, 10 ] + +arr <- c(10, 4, 1, 8, 12, 3) +idx <- c(0, 2, 5) +idx <- idx + 1 # While this is R. +arr_sorted <- arr +arr_sorted[idx] <- sort(arr_sorted[idx]) # Choroba in his blog! +arr +arr_sorted
\ No newline at end of file |
