Check if a Vector Contains a Given Value in R

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

505 Views

We can use match %in% to check whether a vector contains a given value of notExample> x 1%in%x [1] TRUE > 10%in%x [1] TRUE > 99%in%x [1] TRUE > 1024%in%x [1] TRUE > 100%in%x [1] FALSEWe can also do this for checking the common values between two vectors.Example> x y x%in%y [1] FALSE TRUE FALSE FALSE

Drop Factor Levels in Subset of a Data Frame in R

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

791 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" "b" "c" "d" "e" "f" "g" "h" "i" "j"Although we have created a subset but the level of factor variable alphabets still showing 10 levels. If we want to drop the factor levels then it can be done byUsing factor function> subdf$alphabets levels(subdf$alphabets) [1] "a" "b" "c" "d" "e" ... Read More

Create Two Line Charts in the Same Plot in R

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

207 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")

Wrap JSON Using Flexjson in Java

raja
Updated on 06-Jul-2020 12:45:15

1K+ Views

The Flexjson library is a lightweight Java library for serializing and de-serializing java beans, maps, arrays, and collections in a JSON format. A JSONSerializer is the main class for performing serialization of Java objects to JSON and by default performs a shallow serialization. We can wrap a JSON object using the rootName() method of JSONSerializer class, this method wraps the resulting JSON in a javascript object that contains a single field named rootName.Syntaxpublic JSONSerializer rootName(String rootName)Exampleimport flexjson.JSONSerializer; public class JSONRootNameTest {    public static void main(String[] args) {       JSONSerializer serializer = new JSONSerializer().rootName("My_Employee").prettyPrint(true);       Employee emp = new Employee("Adithya", "Jai", 28, ... Read More

Convert Factor to Integer or Numeric Variable in R

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

131 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

Replace NA Values with Zeros in an R Data Frame

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

400 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

Drop Data Frame Columns in R Using Column Name

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

210 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

Extract P-Value and R-Squared from 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

Sort Data Frame in R by Multiple Columns Together

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

336 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

Serialize a List of Objects Using Flexjson in Java

raja
Updated on 06-Jul-2020 12:26:47

1K+ Views

The Flexjson is a lightweight library for serializing and deserializing Java objects into and from JSON format. We can serialize a list of objects using the serialize() method of JSONSerializer class. This method can perform a shallow serialization of the target instance. We need to pass a list of objects of List type as an argument to the serialize() method.Syntaxpublic String serialize(Object target)Exampleimport flexjson.JSONSerializer; import java.util.*; public class JsonSerializeListTest {    public static void main(String[] args) {       JSONSerializer serializer = new JSONSerializer().prettyPrint(true); // pretty print JSON       Student s1 = new Student("Raja", "Ramesh", 28, "Hyderabad");       Student s2 = new Student("Suresh", "Kumar", 30, "Chennai");   ... Read More

Advertisements