Thursday, April 15, 2021

Descriptive statistics in R

 #Tawkir codehub

data<-read.csv(file.choose())  #to input data

summary(data) #to show all data variables

describe(data) #to show mean and median for numeric data

head(data)  #to show some column and variable names


#Rename one column name

colnames(data)[colnames(data)=="lining"]<- "Living"

#Rename all column names

colnames(data)<- c("Age", "Income", "Gender")

#Rename some column names

colnames(data)[colnames(data) %in% c("CO","PMO")]

               

               #Replace value or character in a column

               data$Gender[data$Gender=="Male"]<- "2"

               data$Gender[data$Gender=="Female"]<- "1"

               ##ingeneral variable convert for all data

               data(data=="NA")<- "0"   #Error handeling

               

               #Percentage and frequenty 

               agef<-table(data$Age)  #frequenty table

               agep<-prop.table(table(data$Age)) #percentage table

               barplot(agep, ylab="%")


table(data$Living)

data$Living[data$Living=="Megacity"]<- "1"

data$Living[data$Living=="City"]<- "2"

data$Living[data$Living=="Town"]<- "2"

data$Living[data$Living=="Village"]<- "2"


table(data$Age)

data$Age[data$Age=="Young"]<-"1"

data$Age[data$Age=="Adult"]<-"2"

data$Age[data$Age=="Old"]<-"3"


table(data$Education)

data$Education[data$Education=="Secondary"]<-"1"

data$Education[data$Education=="Tertiary level (college and university degree)"]<-"2"


table(data$Income)

data$Income[data$Income=="Low"]<-"1"

data$Income[data$Income=="Middle"]<-"2"

data$Income[data$Income=="Higher-middle"]<-"3"

data$Income[data$Income=="Higher"]<-"4"


#convert character to number

data$Living<-as.character(data$Living)

data$Age<-as.character(data$Age)

data$Gender<-as.character(data$Gender)

data$Education<-as.character(data$Education)

data$Income<-as.character(data$Income)


data$Living<-as.numeric(as.character(data$Living))

data$Age<-as.numeric(as.character(data$Age))

data$Gender<-as.numeric(as.character(data$Gender))

data$Education<-as.numeric(as.character(data$Education))

data$Income<-as.numeric(as.character(data$Income))

   

data$Living<-as.factor(data$Living)

data$Age<-as.factor(data$Age)

data$Gender<-as.factor(data$Gender)

data$Education<-as.factor(data$Education)

data$Income<-as.factor(data$Income)


#create new data for trial

female<- ifelse(all$Gender=="Female", 1,0)

male<- ifelse(all$Gender=="Male", 2,0)

young<- ifelse(all$Age=="Young",1,0)



describe(data)

summary(data)

colnames(data)



library(psych)

#Calculating_Cronbach's Alpha

demo<- alpha(data.frame(data[c("Living", "Age", "Gender",

                                       "Education", "Income" )]))

#save the change file for future work

write.csv(data,"E:\\planning''''''''''''''''''''''''\\SEM C19 model sir\\chang variable name main\\CODE\\Covid_main.csv", row.names = FALSE)



0 comments:

Post a Comment