<jupyter_start><jupyter_text>**R Programming apply family of functions**In this experiment we will learn about how to use apply family of functionsto do data wrangling and avaoid use of looping constructs The R base manual tells you that it’s called as follows: ```apply(X, MARGIN, FUN, ...)``````X``` is an array or a matrix if the dimension of the array is ```2``````MARGIN``` is a variable defining how the function is applied:  *   When ```MARGIN=1```, it applies over rows *   Whereas with ```MARGIN=2```, it works over columns*   ```MARGIN=c(1,2)```, it applies to both rows and columns;     and```FUN```, represents the function that you want to apply to the data. It can be any R function, including a User Defined Function (UDF).<jupyter_code># Complete the following code by using apropriate apply family function
# Construct a 5x6 matrix
X <-matrix(rnorm(30), nrow=5, ncol=6)
X
# Sum the values of each column with apply()

# Sum the values of each row with apply()

# Complete the following code by using looping construct
# Construct a 5x6 matrix
X <-matrix(rnorm(30), nrow=5, ncol=6)
X
# Sum the values of each column with apply()

A= matrix(
  # taking sequence of elements
  c( 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30),
  # No of rows
  nrow=5,
  # No of columns
  ncol=6,
  byrow=True
)
rownames(A)=c("a","b","c","d","e")
colnames(A)=c("e","f","g","h","i","j")
cat ("The 5*6 matrix:\n")
print (A)
      e  f  g  h   i   j
a     1  2  3  4   5   6
b     7  8  9  10 11   12
c    13 14  15 16 17   18
d    19 20  21 22 23   24
e    25 26 27  28 29   30<jupyter_output><empty_output><jupyter_text>The **lapply()** Function is used when You want to apply a given function to every element of a list and obtain a list as a result.  Syntax of **lapply()** looks like the **apply()** function.There is a slight difference in way they work, which is :1.   **lapply()** can be used for other objects like dataframes, lists or vectors2.   The output returned by **lapply()** is a list (which explains the “l” in the function name), which has the same number of elements as the object passed to it.To see how this works, complete the following code block.**lapply()** is a quite common operation performed on real data when making comparisons or aggregations from different dataframes.<jupyter_code># Using sequence 1:9 create a matrix A with size 3*3

#Using sequence 4:15 create a matrix B with size 4*3

#Using sequnce 8:10 create a matrix C with size 3*2

# create variable MyList as list of matrix A, B, C


# Using lapply and [ operator Extract the 2nd column from `MyList` with the selection operator `[` with `lapply()`

# Using lapply and [ operator Extract the 4th row from `MyList` with the selection operator `[` with `lapply()`


# Using lapply and [ operator Extract the 1st row from `MyList` with the selection operator `[` with `lapply()`<jupyter_output><empty_output><jupyter_text>The **sapply()** FunctionThe **sapply()** function works like **lapply()**, but it tries to simplify the output to the most elementary data structure that is possible. And indeed, ```sapply()``` is a **‘wrapper’** function for **lapply()**.Let’s repeat the extraction operation of a single element as in the last example, but now take the first element of the second row (indexes 2 and 1) for each matrix.Applying the **lapply()** function would give us a list unless you pass ```simplify=FALSE``` as a parameter to **sapply()**. Then, a list will be returned. See how it works in the code chunk below:<jupyter_code># Return a list with `lapply()`
lapply(MyList,"[", 2, 1 )

# Return a vector with `sapply()`
sapply(MyList,"[", 2, 1 )

# Return a list with `sapply()`
sapply(MyList,"[", 2, 1, simplify=F)

# Return a vector with `unlist()`
unlist(lapply(MyList,"[", 2, 1 ))
sapply(x,fUN)
Arguments:-x:A vector or an object
-Fun:Function applied to each element of x
dt<-cars
lmmn_cars <-lapply(dt,min)
smn_cars <- sapply(dt,min)
smn_cars<jupyter_output><empty_output><jupyter_text>In this Text Block Write note on mapply function and give exampe code and exaplain use of mapply function.1.<jupyter_code># Example use of mapply function
mapply (rep,1:5,5:1)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11675726778539353, 'fraction_numerical': 0.037107066887260697, 'mean_word_length': 4.58311345646438, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '117704', 'license': 'Apache-2.0', 'n_tokens_mistral': 1460, 'n_tokens_neox': 1346, 'n_words': 607, 'path': 'R_Experiment4_writeup.ipynb', 'repo_name': 'mahajantwinkle99/add-to-gitnor', 'size': '4231'}