Programming Articles - Page 1737 of 3363

How to sort a vector in increasing order that contains numbers and characters in R?

Nizamuddin Siddiqui
Updated on 04-Sep-2020 07:35:05

249 Views

A vector can contain numbers, characters or both. The sorting of vectors that contain only numbers or only characters is not very difficult but if a vector contains both of them then it is a little tedious task. In R, we can sort a vector that contains numbers as well as characters with the help of order function but before doing this sorting we must look at the vector very carefully to check if the characters are different for the elements of the vector or not, if they are different then we can’t do this sorting in the manner explained ... Read More

How to create a row at the end an R data frame with column totals?

Nizamuddin Siddiqui
Updated on 04-Sep-2020 07:32:57

296 Views

In data analysis, we often need column totals, especially in situations where we want to perform the analysis in a step by step manner. There are many analytical techniques in which we find the column totals such as ANALYSIS OF VARIANCE, CORRELATION, REGRESSION, etc. To find the column totals, we can use colSums function and use the single square brackets to put these totals as a row in the data frame.Example1Consider the below data frame − Live Demo> x1 x2 x3 df1 df1Output  x1 x2 x3 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 ... Read More

How to select the first and last row based on group column in an R data frame?

Nizamuddin Siddiqui
Updated on 04-Sep-2020 07:30:13

1K+ Views

Extraction of data is necessary in data analysis because extraction helps us to keep the important information about a data set. This important information could be the first row and the last row of groups as well, also we might want to use these rows for other type of analysis such as comparing the initial and last data values among groups. We can extract or select the first and last row based on group column by using slice function of dplyr package.Example Live DemoConsider the below data frame: > x1 x2 df1 head(df1, 12)Output  x1 x2 1  1  3 2  1 ... Read More

How to get the list of data sets available in base R or in a package in R?

Nizamuddin Siddiqui
Updated on 04-Sep-2020 07:27:32

8K+ Views

There are many data sets available in base R and in different packages of R. The characteristics of these data sets are very different, for example, some data sets are time series data, some have only numerical columns, some have numerical as well as factor columns, some includes character columns with other type of columns. Therefore, it becomes helpful to everyone who want to learn the use of R programming. To get the list of available data sets in base R we can use data() but to get the list of data sets available in a package we first need ... Read More

How to plot multiple time series using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 04-Sep-2020 06:58:38

1K+ Views

For a one point of time, we might have multiple time series data, this could be weather for multiple cities, price variation in multiple products, demand expectancy at different locations, or anything that changes with time and measured for multiple things or locations. If we have such type of time series data then we would be needing to plot that data in a single plot and it can be done with the help of geom_line function of ggplot2 package.ExampleConsider the below data frames − Live Demo> x1 y1 df1 df1Output   x1 y1 1 1 -0.1165387 2 2 -0.9084062 3 3 0.4696637 ... Read More

How to create vector in R with a sequence of numbers?

Nizamuddin Siddiqui
Updated on 04-Sep-2020 08:38:20

21K+ Views

Creating a numeric vector is the first step towards learning R programming and there are many ways to do that but if we want to generate a sequence of number then it is a bit different thing, not totally different. We can create a vector with a sequence of numbers by using − if the sequence of numbers needs to have only the difference of 1, otherwise seq function can be used.Example Live Demo> x1 x1Output[1] 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 ... Read More

Changing Directions in Python

Arnab Chakraborty
Updated on 02-Sep-2020 13:28:44

938 Views

Suppose we have a list of numbers called nums, we have to find the number of times that the list changes from positive-to-negative or negative-to-positive slope.So, if the input is like [2, 4, 10, 18, 6, 11, 13], then the output will be 2, as it changes the direction at 10 (positive-to-negative), and then at 6 (negative-to-positive).To solve this, we will follow these steps −To solve this, we will follow these steps −for i in range 1 to size of nums - 1, doif nums[i-1] < nums[i] > nums[i+1] or nums[i-1] > nums[i] < nums[i+1], thencount := count + 1return ... Read More

Cell fusion in Python

Arnab Chakraborty
Updated on 02-Sep-2020 13:25:50

216 Views

Suppose we have a list of numbers called cells; this list is representing sizes of different cells. Now, in each iteration, the two largest cells a and b interact according to these rules: So, If a = b, they both die. Otherwise, the two cells merge and their size becomes floor of ((a + b) / 3). We have to find the size of the last cell or return -1 if there's no cell is remaining.So, if the input is like [20, 40, 40, 30], then the output will be 16, in first iteration, 40 and 40 will die, then ... Read More

camelCase in Python

Arnab Chakraborty
Updated on 02-Sep-2020 13:21:45

9K+ Views

Suppose we have a list of words, we have to concatenate them in camel case format.So, if the input is like ["Hello", "World", "Python", "Programming"], then the output will be "helloWorldPythonProgramming"To solve this, we will follow these steps −s := blank stringfor each word in words −make first letter word uppercase and rest lowercaseconcatenate word with sret := s by converting first letter of s as lowercasereturn retLet us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, words):       s = "".join(word[0].upper() + word[1:].lower() for word in words)       return ... Read More

Caesar Cipher in Python

Arnab Chakraborty
Updated on 02-Sep-2020 13:20:03

1K+ Views

Suppose we have a lowercase alphabet string s, and an offset number say k. We have to replace every letter in s with a letter k positions further along the alphabet. We have to keep in mind that when the letter overflows past a or z, it gets wrapped around the other side.So, if the input is like "hello", k = 3, then the output will be "khoor"To solve this, we will follow these steps −Define a function shift(). This will take ci := ASCII of (c) - ASCII of ('a')i := i + ki := i mod 26return character ... Read More

Advertisements