Found 33676 Articles for Programming

How to convert a factor that is represented by numeric values to integer or numericvariable in R?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 12:43:51

126 Views

We can convert a factor to integer or numeric variable by using as.numeric function with defining the levels of the factor or by defining the characters of the factorExample> f f [1] 0.323049098020419 0.916131897130981 0.271536672720686 0.462429489241913 [5] 0.657008627429605 0.462429489241913 0.462429489241913 0.212830029195175 [9] 0.271536672720686 0.497305172728375 7 Levels: 0.212830029195175 0.271536672720686 ... 0.916131897130981Using as.numeric> as.numeric(levels(f))[f] [1] 0.3230491 0.9161319 0.2715367 0.4624295 0.6570086 0.4624295 0.4624295 [8] 0.2128300 0.2715367 0.4973052 > Using as.numeric(as.character( )) > as.numeric(as.character(f)) [1] 0.3230491 0.9161319 0.2715367 0.4624295 0.6570086 0.4624295 0.4624295 [8] 0.2128300 0.2715367 0.4973052

How to replace NA values with zeros in an R data frame?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 12:40:55

391 Views

We can replace all NA values by using is.na functionExample> Data df df V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 1 9 7 3 0 3 7 7 3 9 9 2 9 2 3 0 2 0 1 4 6 7 3 5 0 9 2 4 8 8 7 NA 5 4 7 3 1 2 6 NA 7 1 1 8 5 3 2 9 6 4 7 0 5 6 1 6 8 5 6 5 3 9 6 0 7 0 7 8 3 4 NA NA 0 2 4 2 NA 8 6 9 9 9 4 0 6 1 7 NA 9 5 5 NA 8 1 NA 0 9 9 3 10 1 1 0 7 1 1 4 1 2 1Replacing NA’s by 0’s> df[is.na(df)] df V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 1 9 7 3 0 3 7 7 3 9 9 2 9 2 3 0 2 0 1 4 6 7 3 5 0 9 2 4 8 8 7 0 5 4 7 3 1 2 6 0 7 1 1 8 5 3 2 9 6 4 7 0 5 6 1 6 8 5 6 5 3 9 6 0 7 0 7 8 3 4 0 0 0 2 4 2 0 8 6 9 9 9 4 0 6 1 7 0 9 5 5 0 8 1 0 0 9 9 3 10 1 1 0 7 1 1 4 1 2 1

How to drop data frame columns in R by using column name?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 12:39:01

201 Views

Columns of a data frame can be dropped by creating an object of columns that we want to drop or an object of columns that we want to keep.Example> df keeps df[keeps] Var3 Var4 1 21 31 2 22 32 3 23 33 4 24 34 5 25 35 6 26 36 7 27 37 8 28 38 9 29 39 10 30 40

How to extract p-value and R-squared from a linear regression in R?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 12:36:36

2K+ Views

We can use regression model object name with $r.squared to find the R-squared and a user defined function to extract the p-value.ExampleExtracting R-Squared> x y LinearRegression summary(LinearRegression)$r.squared [1] 0.2814271Extracting p-value> Regressionp

How to sort a data frame in R by multiple columns together?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 12:33:58

329 Views

We can sort a data frame by multiple columns using order function.ExampleConsider the below data frame −> df df x1 x2 x3 x4 1 Hi A 4 9 2 Med B 7 5 3 Hi D 5 7 4 Low C 3 4Let’s say we want to sort the data frame by column x4 in descending order then by column x1 in ascending order.It can be done follows −> df[with(df, order(-x4, x1)), ] x1 x2 x3 x4 1 Hi A 4 9 3 Hi D 5 7 2 Med B 7 5 4 Low C 3 4We can do ... Read More

Python Group elements at same indices in a multi-list

Hafeezul Kareem
Updated on 06-Jul-2020 09:40:01

686 Views

In this tutorial, we are going to write a program that combines elements of the same indices different lists into a single list. And there is one constraint here. All the lists must be of the same length. Let's see an example to understand it more clearly.Input[[1, 2, 3], [4, 5, 6], [7, 8, 9]]Output[[1, 4, 7], [2, 5, 8], [3, 6, 9]]We can solve it in different ways. Let's see how to solve with normal loops.Initialize the list with lists.Initialize an empty list.Initialize a variable index to 0.Iterate over the sub list length timesAppend an empty list to the ... Read More

Python Group Anagrams from given list

Hafeezul Kareem
Updated on 06-Jul-2020 09:37:28

4K+ Views

In this tutorial, we are going to write a program that groups all anagrams in a list. First, let's see what are anagrams.Any two strings that have the same character in a different order are known as anagrams.Before diving into the solution, let's see an example.Input['cat', 'dog', 'fired', 'god', 'pat', 'tap', 'fried', 'tac']Output[['cat', 'tac'], ['dog', 'god'], ['fried', 'fired'], ['pat', 'tap']]We will breakdown the problem into two pieces. First, we will write a function that checks two strings are anagrams or not. Follow the below steps to write code to check anagrams.Initialize the strings.Sort both the strings.If both sorted strings are ... Read More

Python Getting sublist element till N

Hafeezul Kareem
Updated on 06-Jul-2020 09:35:59

246 Views

In this tutorial, we are going to write a program that returns a sublist element till nth sublist in a list. Let's say we have the following list with 5 sublists.[['Python', 'Java'], ['C', 'Pascal'], ['Javascript', 'PHP'], ['C#', 'C++'], ['React', 'Angular']] Now, we have to get the first element from the first three sublists. We can get the elements different approaches. Let's see some of them.LoopsMore generic and first thought of the most of programmers is to use loops. Let's see the code using loops.Example Live Demo# initializing the list and N random_list = [['Python', 'Java'], ['C', 'Pascal'], ['Javascript', 'PHP'], ['C# C++'], ... Read More

Program to check congruency of two triangles in Python

Hafeezul Kareem
Updated on 06-Jul-2020 09:34:32

315 Views

In this tutorial, we are going to check the congruency of two triangles. We are going to check SSS, SAS, and AAA. The similarity of the triangles is proved based on those criteria.We have to check different conditions based on the theorem. Check them in the code below.Example Live Demodef side_side_side(sides_one, sides_two):    # sorting same pace    sides_one.sort()    sides_two.sort()    # checking the conditions    if sides_one[0] / sides_two[0] == sides_one[1] / sides_two[1] \       and sides_one[1] / sides_two[1] == sides_one[2] / sides_two[2] \       and sides_one[2] / sides_two[2] == sides_one[0] / sides_two[0]:     ... Read More

Program for longest common directory path in Python

Hafeezul Kareem
Updated on 06-Jul-2020 09:28:32

471 Views

In this tutorial, we are going to write a program that finds the longest common path from the given list of paths. Let's see an example to understand the problem statement more clearly.Inputpaths = ['home/tutorialspoint/python', 'home/tutorialspoint/c', 'home/tutorialspoint/javascript', 'home/tutorialspoint/react', 'home/tutorialspoint/django']/home/tutorialspoint/We can solve the problem using os module very easily. Let's see the steps to solve theImport the os module.Initialize the list of paths to find the longest common path.Find the common prefix of all paths using os.path.commonprefix(paths) and store it in variable.And extract the directory from the common prefix using os.path.dirname(common_prefix).Example Live Demo# importing the os module import os # initializing the ... Read More

Advertisements