Sum Variable by Factor Levels in R

Nizamuddin Siddiqui
Updated on 06-Jul-2020 14:08:10

3K+ Views

We can do this by using aggregate function or with the help tapplyExample> x x Category Frequency 1 Graduation 12 2 Graduation 19 3 Post-Graduation 15 4 Graduation 20 5 PhD 25 6 Post-Graduation 13 7 PhD 14Using aggregate> aggregate(x$Frequency, by=list(Group=x$Category), FUN=sum) Group x 1 Graduation 51 2 PhD 39 3 Post-Graduation 28 Using tapply > tapply(x$Frequency, x$Category, FUN=sum) Graduation PhD Post-Graduation 51 39 28

Extract Columns of a Data Frame in R using dplyr Package

Nizamuddin Siddiqui
Updated on 06-Jul-2020 14:06:29

337 Views

Example> x y z df library(dplyr) > df %>% select(y, z) y z 1 1.486720e-06 7.888609e-31 2 1.338302e-04 7.888609e-29 3 4.431848e-03 3.904861e-27 4 5.399097e-02 1.275588e-25 5 2.419707e-01 3.093301e-24 6 3.989423e-01 5.939138e-23 7 2.419707e-01 9.403635e-22 8 5.399097e-02 1.262774e-20 9 4.431848e-03 1.467975e-19 10 1.338302e-04 1.500596e-18 11 1.486720e-06 1.365543e-17 12 6.075883e-09 1.117262e-16 13 9.134720e-12 8.286361e-16 14 5.052271e-15 5.609229e-15 15 1.027977e-18 3.485735e-14 16 7.694599e-23 1.998488e-13 17 2.118819e-27 1.061697e-12 18 2.146384e-32 5.246031e-12 19 7.998828e-38 2.419003e-11 20 1.096607e-43 1.043991e-10 21 5.530710e-50 4.228163e-10Second> select(df, y, z) y z 1 1.486720e-06 7.888609e-31 2 1.338302e-04 7.888609e-29 3 4.431848e-03 3.904861e-27 4 5.399097e-02 1.275588e-25 5 2.419707e-01 3.093301e-24 6 ... Read More

Delete Useless Images in WhatsApp Automatically

Lakshmi Srinivas
Updated on 06-Jul-2020 13:37:01

2K+ Views

Probably WhatsApp is the most memory-consuming instant messenger today. As if Good Morning messages with hot cup of coffee and a croissant or some chirping bird were insufficient, the enthusiasts are creating and sending messages for greeting “Good Afternoon” and “Good Night” too. These images and other media files start accumulating in chunks especially if you have a setting of Auto Download. These useless images eat up large space of our mobile devices leave very little space for their smart use. At times they also slow down the device performance. At some point of time it becomes very annoying.The simplest ... Read More

Use of JacksonInject Annotation in Java

raja
Updated on 06-Jul-2020 13:35:24

651 Views

The Jackson @JacksonInject annotation can be used to inject the values into parsed objects instead of reading those values from the JSON. In order to inject values into a field, we can use the InjectableValues class and need to configure the ObjectMapper class to read both the injected values from the InjectableValues class and the remaining values from the JSON string.Syntax@Target(value={ANNOTATION_TYPE, METHOD, FIELD, PARAMETER}) @Retention(value=RUNTIME) public @interface JacksonInjectExampleimport com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.databind.*; import java.io.*; public class JacksonInjectTest {    public static void main(String args[]) throws IOException {       String jsonString = "{\"empName\": \"Raja Ramesh\"}";       InjectableValues injectableValues = new InjectableValues.Std().addValue(int.class, 110); ... Read More

Ignore Class During Serialization Using Jackson in Java

raja
Updated on 06-Jul-2020 13:29:57

3K+ Views

The Jackson @JsonIgnoreType annotation can be used to ignore a class during the serialization process and it can mark all the properties or fields of a class to be ignored while serializing and deserializing a JSON object.Syntax@Target(value={ANNOTATION_TYPE, TYPE}) @Retention(value=RUNTIME) public @interface JsonIgnoreTypeExampleimport com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.databind.*; import java.io.*; public class JsonIgnoreTypeTest {    public static void main(String args[]) throws IOException {       Employee emp = new Employee();       ObjectMapper mapper = new ObjectMapper();       String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(emp);       System.out.println(jsonString);    } } // Employee class class Employee {    @JsonIgnoreType    public static class Address {   ... Read More

Add JSON String to Existing JSON File in Java

raja
Updated on 06-Jul-2020 13:21:04

6K+ Views

A Gson is a json library for Java and it can be used to generate a JSON. In the initial step, we can read a JSON file and parsing to a Java object then need to typecast the Java object to a JSonObject and parsing to a JsonArray. Then iterating this JSON array to print the JsonElement. We can create a JsonWriter class to write a JSON encoded value to a stream, one token at a time. Finally, a new JSON string can be written to an existing json file. Exampleimport java.io.*; import java.util.*; import com.google.gson.*; import com.google.gson.stream.*; import com.google.gson.annotations.*; public class JSONFilewriteTest {   ... Read More

Ignore Multiple Properties of a JSON Object in Java

raja
Updated on 06-Jul-2020 13:18:16

4K+ Views

The @JsonIgnoreProperties Jackson annotation can be used to specify a list of properties or fields of a class to ignore. The @JsonIgnoreProperties annotation can be placed above the class declaration instead of above the individual properties or fields to ignore.Syntax@Target(value={ANNOTATION_TYPE, TYPE, METHOD, CONSTRUCTOR, FIELD}) @Retention(value=RUNTIME) public @interface JsonIgnorePropertiesExampleimport java.io.*; import com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.databind.*; public class JsonIgnorePropertiesTest {    public static void main(String[] args) throws IOException {       Customer customer = new Customer("120", "Ravi", "Hyderabad");       System.out.println(customer);       ObjectMapper mapper = new ObjectMapper();       String jsonString = mapper.writeValueAsString(customer);       System.out.println("JSON: " + jsonString);     ... Read More

Rename JSON Properties Using Gson in Java

raja
Updated on 06-Jul-2020 13:17:10

4K+ Views

The Gson @SerializedName annotation can be serialized to a JSON with the provided name value as its field name. This annotation can override any FieldNamingPolicy including the default field naming policy that may have been set on the Gson instance. A different naming policy can set using the GsonBuilder class.Syntax@Retention(value=RUNTIME) @Target(value={FIELD, METHOD}) public @interface SerializedNameExampleimport com.google.gson.annotations.*; import com.google.gson.*; public class SerializedNameAnnotationTest {    public static void main(String args[]) {       Employee emp = new Employee("Rahul", "Dev", 30, "Nagpur");       Gson gson = new GsonBuilder().setPrettyPrinting().create(); // pretty print       String jsonStr = gson.toJson(emp);       System.out.println(jsonStr);    } ... Read More

When to Use @JsonAutoDetect Annotation in Java

raja
Updated on 06-Jul-2020 13:16:17

5K+ Views

The @JsonAutoDetect annotation can be used at the class level to override the visibility of the properties of a class during serialization and deserialization. We can set the visibility with the properties like "creatorVisibility", "fieldVisibility", "getterVisibility", "setterVisibility" and "isGetterVisibility". The JsonAutoDetect class can define public static constants that are similar to Java class visibility levels like "ANY", "DEFAULT", "NON_PRIVATE", "NONE", "PROTECTED_AND_PRIVATE" and "PUBLIC_ONLY".Exampleimport com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.databind.*; import java.io.*; public class JsonAutoDetectTest {    public static void main(String[] args) throws IOException {       Address address = new Address("Madhapur", "Hyderabad", "Telangana");       Name name = new Name("Raja", "Ramesh");       Student student ... Read More

Ignore Field of JSON Object Using Jackson Library in Java

raja
Updated on 06-Jul-2020 13:12:32

9K+ Views

The Jackson @JsonIgnore annotation can be used to ignore a certain property or field of a Java object. The property can be ignored both when reading JSON into Java objects and when writing Java objects into JSON. We can use the readValue() and writeValueAsString() methods of ObjectMapper class to read a JSON to Java Object and to write a Java object to JSON.Syntax@Target(value={ANNOTATION_TYPE, METHOD, CONSTRUCTOR, FIELD}) @Retention(value=RUNTIME) public @interface JsonIgnoreExampleimport java.io.*; import com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.databind.*; public class JsonIgnoreTest {    public static void main(String[] args) throws IOException {       Customer customer = new Customer("110", "Surya Kiran", "Chennai");       System.out.println(customer); ... Read More

Advertisements