Replace All 0's with 5 in a Given Number using Python

Dev Prakash Sharma
Updated on 05-Feb-2021 10:20:10

657 Views

Given an Integer N the task is to replace all the 0’s that appear in the number with ‘5’. However, the number with leading ‘0’ cannot be replaced with ‘5’ as it remains unchanged. For example, Input-1 −N = 1007Output −1557Explanation − The given number has 2 zeros which when replaced by ‘5’ results in the output as 1557.Input-2 −N = 00105Output −155Explanation − Since the given number starts with the leading ‘0’ which can be ignored and the output after replacing the 0 in the middle with ‘5’ results the output as 155.Approach to solve this problemTo replace all ... Read More

Convert Matrix Rows into a List in R

Nizamuddin Siddiqui
Updated on 05-Feb-2021 10:17:13

2K+ Views

Depending on our objective, a matrix rows might be needed to converted into a list that means each row will be an element of the list. This can be done by using the function as.list but firstly we need to convert the matrix into data frame after transposing. For example, if we have a matrix called M then it’s rows will be converted to a list using the command writtem below −as.list(data.frame(t(M)))Example1 Live DemoM1

Create Bins for a Continuous Vector in R

Nizamuddin Siddiqui
Updated on 05-Feb-2021 10:11:01

3K+ Views

To create the bins for a continuous vector, we can use cut function and store the bins in a data frame along with the original vector. The values in the cut function must be passed based on the range of the vector values, otherwise, there will be NA’s in the bin values. For example, if we have a vector that contains 0.55 and we do not use 0 in the cut function then the first bin will be NA. Check out the below examples to understand how to properly do it.Example1 Live Demox1

Convert Character Data Frame to Numeric Data Frame in R

Nizamuddin Siddiqui
Updated on 05-Feb-2021 10:08:30

2K+ Views

Sometimes numerical values are recorded as character values and we need to convert them to numeric type before starting our analysis. This is also possible for a whole data frame in R. Therefore, we can use sapply function to convert the columns of the data frame to numeric type and save the output in a data frame by reading it with as.data.frame.Example1Consider the below data frame − Live Demox

Create Boxplot Without Frame in Base R

Nizamuddin Siddiqui
Updated on 05-Feb-2021 10:05:39

933 Views

The boxplot in base R is covered with a box and that box is called a frame. We can get rid of that frame by using frame argument while creating the boxplot. For example, if we have a vector called x and we want to create the boxplot without frame then it can be done by using the command boxplot(x,frame=F). This will remove all the sides of the boxplot except the Y−axis labels because this will help us to understand the distribution of the variable.Example Live Demox

Plot Vector with Missing Values in Base R

Nizamuddin Siddiqui
Updated on 05-Feb-2021 10:03:42

362 Views

If there are missing values in a vector then the plot of such vector will not have all the values, only the non−missing values will be shown. If we want to create the plot by adding the missing values in the plot then we need to define the X−axis for the length of the vector and the Y−axis with the actual vector using cbind but missing values will be omitted as shown in the below example.Example Live Demox

Print Large Number of Values in R Without Index

Nizamuddin Siddiqui
Updated on 05-Feb-2021 10:00:33

1K+ Views

Whenever we print any vector then the left-hand side of the R window shows indexing, even if we have one value in the vector the index will be there. For example, if we print a vector that has value equal to 2 then the print output will be [1] 2, here [1] represents the index. If we want to print the vector without index then cat function can be used as shown in the below examples.Example1 Live Demox1

Round Exponential Numbers in R

Nizamuddin Siddiqui
Updated on 05-Feb-2021 09:56:22

1K+ Views

The exponential numbers are also called scientific numbers and these numbers have exponent representation by the letter e. For example, a number 12340000 can be represented as 1.234e + 107. We can round this to 1.2e + 107 and in R it can be done with the help of singif function.Example1 Live Demox1

Create a Vector with All Dates in a Particular Year in R

Nizamuddin Siddiqui
Updated on 05-Feb-2021 09:33:50

1K+ Views

We know that some years are leap years and some are normal years. The leap years have 366 days and the normal years have 365 days. To create a vector with all dates in a particular year we can use first date and the last date of the year by reading them with as.Date and creating a sequence with seq function. Check out the below examples to understand how it is done.Example1Creating a vector with dates in year 2020 − Live Demoseq(as.Date("2020-01-01"), as.Date("2020-12-31"), by="1 day")Output[1] "2020−01−01" "2020−01−02" "2020−01−03" "2020−01−04" "2020−01−05" [6] "2020−01−06" "2020−01−07" "2020−01−08" "2020−01−09" "2020−01−10" [11] "2020−01−11" "2020−01−12" "2020−01−13" "2020−01−14" ... Read More

Change Color of X-Axis Label Using ggplot2 in R

Nizamuddin Siddiqui
Updated on 05-Feb-2021 09:05:04

8K+ Views

The default color of labels is black but we might want to change that color to something else so that we can get attention of the viewer to the labels if it is needed. To change the color of X-axis label using ggplot2, we can use theme function that has axis.title.x argument which can be used for changing the color of the label values.ExampleConsider the below data frame − Live Demox

Advertisements