To divide each value in a data frame by column total, we can use apply function and define the function for the division. For example, if we have a data frame called df that contains five columns then we can divide each value of these columns by column total using the command apply(df,2,function(x){x/sum(x)})ExampleConsider the below data frame − Live Demox1
To find the sum by two factor columns, we can use aggregate function. This is mostly required when we have frequency/count data for two factors. For example, if we have a data frame called df that contains two factor columns say f1 and f2 and one numerical column say Count then the sum of Count by f1 and f2 can be calculated by using the command aggregate(Count~f1+f2,data=df,sum).ExampleConsider the below data frame − Live Demox1
To convert an array into a matrix in R, we can use apply function. For example, if we have an array called ARRAY that contains 2 array elements then we can convert this array into a single matrix using the command apply(ARRAY,2,c). We need to understand the dimension of the array making the conversion otherwise the output will not be as expected.ExampleConsider the below array − Live Demox1
A data frame can be created by using data.frame function but in this case R generates the column names using the values we pass for the data. For example, if we pass a probability distribution as shown in the below examples then its name will be there. If we want to change those names then setNames function can be used along with the data frame name.Example Live Demodf1
To find the position of odd numbers in an R vector, we can find the position of values that are divisible by 2 with the help of which function. For example, if we have a vector called x then we can find the position of odd numbers using the command which(x%%2==1). Check out the below examples to understand how it works.Example Live Demox1
%matplotlib would return the backend value.%matplotlib auto would return the name of the backend, over Ipython shell.ExampleIn [1]: %matplotlib autoOutputUsing matplotlib backend: GTK3Agg
In this problem, we are given a doubly linked list and a value sum. Our task is to find pairs with a given sum in a doubly linked list.Let’s take an example to understand the problem, Inputhead − 2 5 6 9 12 x = 11Output(2, 9), (5, 6)ExplanationFor pairs (2, 9), the sum of values is 11 For pairs (5, 6), the sum of values is 11Solution ApproachA simple solution to the problem is traversing the whole linked-list and taking elements one by one and finding the element in the remaining linked list whose sum ... Read More
To generate random numbers with sequence and storing them in a data frame column, we can use sample function with seq. For example, if we want to create a data frame column having random sequence of values of size 50 between 1 to 100 by considering every tenth value then we can use the commanddf
To create a random vector for a range of values, we can use sample function. We just need to pass the range and the sample size inside the sample function. For example, if we want to create a random sample of size 20 for a range of values between 1 to 100 then we can use the command sample(1:100,20) and if the sample size is larger than 100 then we can add replace=TRUE as shown in the below examples.Example Live Demox1
To create a table for the number of unique values in list of vectors, we can use mtabulate function of qdapTools package. For example, if we have a list of vectors say LIST that contains some vectors then the table for the number of unique values in the vectors of LIST can be found by using mtabulate(LIST).ExampleConsider the below list − Live Demox1