Saturday, January 7, 2023

Correct Structure of your data

 The correct data structure is very important during the analysis of your data. The treatment variable must be as a factor and measured parameters into numbers. In this lesson, we will convert the treatment variable into factors

#-- View the structure of your data

setwd("D:/R_working/script_book")

data_soil=read.csv("data_soil.csv",na="***")

data_soil=data.frame(data_soil)


View the structure of data

str(data_soil)

## ’data.frame’: 72 obs. of 10 variables:

## $ irrigation : chr "normal" "normal" "normal" "skip" ...

## $ mulches : chr "control" "control" "control" "control" ...

## $ nitrogen_rates : int 0 0 0 0 0 0 75 75 75 75 ...

## $ replication : int 1 2 3 1 2 3 1 2 3 1 ...

## $ Organic_matter : num 0.46 0.44 0.45 0.4 0.39 0.41 0.49 0.53 0.51 0.46 ...

## $ water_contents : num 31.4 31.6 31.9 28.3 28.1 ...

## $ soil_phosphorus : num 8.51 8.54 8.54 8.5 8.46 8.54 8.56 8.55 8.57 8.54 ...

## $ soil_potassium : num 191 189 190 184 186 185 193 191 195 188 ...

## $ electrical_conductivity: num 2.7 2.65 2.59 2.2 2.3 2.15 2.77 2.6 2.66 2.15 ...

## $ PH : num 8.2 8 8.1 8.1 8.1 8 8 8.2 8.1 8 ...

You can see from the output results of commands that R is reading our treatment variables: irrigation, mulches, nitrogen rates, and replications as characters and integers. So, we will convert them into factors with the following commands:


# Convert your treatments variables into factors

data_soil <- within(data_soil, {

irrigation <- factor(irrigation)

mulches <- factor(mulches)

nitrogen_rates <- factor(nitrogen_rates)

replication <- factor(replication )

})

Now again view the structure of your data

str(data_soil)


## ’data.frame’: 72 obs. of 10 variables:

## $ irrigation : Factor w/ 2 levels "normal","skip": 1 1 1 2 2 2 1 1 1 2 ...

## $ mulches : Factor w/ 4 levels "control","plastic_mulch",..: 1 1 1 1 1 1 1 1 1 1 ...

## $ nitrogen_rates : Factor w/ 3 levels "0","75","150": 1 1 1 1 1 1 2 2 2 2 ...

## $ replication : Factor w/ 3 levels "1","2","3": 1 2 3 1 2 3 1 2 3 1 ...

## $ Organic_matter : num 0.46 0.44 0.45 0.4 0.39 0.41 0.49 0.53 0.51 0.46 ...

## $ water_contents : num 31.4 31.6 31.9 28.3 28.1 ...

## $ soil_phosphorus : num 8.51 8.54 8.54 8.5 8.46 8.54 8.56 8.55 8.57 8.54 ...

## $ soil_potassium : num 191 189 190 184 186 185 193 191 195 188 ...

## $ electrical_conductivity: num 2.7 2.65 2.59 2.2 2.3 2.15 2.77 2.6 2.66 2.15 ...

## $ PH : num 8.2 8 8.1 8.1 8.1 8 8 8.2 8.1 8 ...


No comments:

Post a Comment