Nizamuddin Siddiqui has Published 2307 Articles

How to drop factor levels in subset of a data frame in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 06-Jul-2020 12:47:10

494 Views

There are two ways to do drop the factor levels in a subset of a data frame, first one is by using factor function and another is by using lapply.Example> df levels(df$alphabets) [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" > subdf levels(subdf$alphabets) [1] "a" ... Read More

How to create two line charts in the same plot in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 06-Jul-2020 12:45:16

139 Views

We can do this by using lines function after plotting the first chart.Example> x X1 X2 plot(x, X1, type="l",col="red", ylab="X") > lines(x, X2, col="green")

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

Nizamuddin Siddiqui

Nizamuddin Siddiqui

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

55 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 ... ... Read More

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

Nizamuddin Siddiqui

Nizamuddin Siddiqui

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

294 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 ... Read More

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

Nizamuddin Siddiqui

Nizamuddin Siddiqui

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

125 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

Nizamuddin Siddiqui

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

1K+ 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

Nizamuddin Siddiqui

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

229 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 ... Read More

Advertisements