0% found this document useful (0 votes)
102 views

Dataframe Concept in R-Programming

The document contains code for creating an R data frame called "employees" with columns for name, age, height, child status, and smoking status. It then demonstrates various operations on the data frame such as subsetting rows and columns, binding additional columns, and sorting rows by age.

Uploaded by

Mahaboob Basha
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views

Dataframe Concept in R-Programming

The document contains code for creating an R data frame called "employees" with columns for name, age, height, child status, and smoking status. It then demonstrates various operations on the data frame such as subsetting rows and columns, binding additional columns, and sorting rows by age.

Uploaded by

Mahaboob Basha
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

> name<-c("Ram", "Raj", "Rajesh", "Raheem", "Rahul")

> age<-c("26", "28", "30", "35", "32")


> height<-c("5.7", "5.9", "5.6", "5.5", "5.8")
> child<-c("TRUE", "FALSE", "TRUE", "FALSE", "TRUE")
>
> data.frame(name, age, height, child)
name age height child
1 Ram 26 5.7 TRUE
2 Raj 28 5.9 FALSE
3 Rajesh 30 5.6 TRUE
4 Raheem 35 5.5 FALSE
5 Rahul 32 5.8 TRUE
> employees<-data.frame(name, age, height, child)
> employees
name age height child
1 Ram 26 5.7 TRUE
2 Raj 28 5.9 FALSE
3 Rajesh 30 5.6 TRUE
4 Raheem 35 5.5 FALSE
5 Rahul 32 5.8 TRUE
> employees[3, 2]
[1] 30
Levels: 26 28 30 32 35
> employees[3, "age"]
[1] 30
Levels: 26 28 30 32 35
> employees[3,]
name age height child
3 Rajesh 30 5.6 TRUE
> employees[,3]
[1] 5.7 5.9 5.6 5.5 5.8
Levels: 5.5 5.6 5.7 5.8 5.9
> employees
name age height child
1 Ram 26 5.7 TRUE
2 Raj 28 5.9 FALSE
3 Rajesh 30 5.6 TRUE
4 Raheem 35 5.5 FALSE
5 Rahul 32 5.8 TRUE
> employees(3, 4), c("age", "child")
Error: unexpected ',' in "employees(3, 4),"
> employees[c(3, 4), c("age", "child")]
age child
3 30 TRUE
4 35 FALSE
> employees[c(4,5), c("name", "age", "height")]
name age height
4 Raheem 35 5.5
5 Rahul 32 5.8
> employees[c(1,2), c("name", "age")]
name age
1 Ram 26
2 Raj 28
> employees[c(2,5), c("age", "child")]
age child
2 28 FALSE
5 32 TRUE
> employees[2]
age
1 26
2 28
3 30
4 35
5 32
> employees[,2]
[1] 26 28 30 35 32
Levels: 26 28 30 32 35
> employees[2]
age
1 26
2 28
3 30
4 35
5 32
> employees[3]
height
1 5.7
2 5.9
3 5.6
4 5.5
5 5.8
> employees[4]
child
1 TRUE
2 FALSE
3 TRUE
4 FALSE
5 TRUE
> employees$age
[1] 26 28 30 35 32
Levels: 26 28 30 32 35
> employees$child
[1] TRUE FALSE TRUE FALSE TRUE
Levels: FALSE TRUE
> employees$name
[1] Ram Raj Rajesh Raheem Rahul
Levels: Raheem Rahul Raj Rajesh Ram
> employees["age"]
age
1 26
2 28
3 30
4 35
5 32
> employees[["age"]]
[1] 26 28 30 35 32
Levels: 26 28 30 32 35
> employees[2]
age
1 26
2 28
3 30
4 35
5 32
> employees[[2]]
[1] 26 28 30 35 32
Levels: 26 28 30 32 35
> employees["age"]
age
1 26
2 28
3 30
4 35
5 32
> employees[2]
age
1 26
2 28
3 30
4 35
5 32
> smoking<-c("yes", "no", "yes", "yes", "no")
> smoking
[1] "yes" "no" "yes" "yes" "no"
> employees$smoking<-smoking
> employees["smoking"]<-smoking
> employees
name age height child smoking
1 Ram 26 5.7 TRUE yes
2 Raj 28 5.9 FALSE no
3 Rajesh 30 5.6 TRUE yes
4 Raheem 35 5.5 FALSE yes
5 Rahul 32 5.8 TRUE no
> weight<-c(50, 60, 70, 50, 60)
> cbind(employees$weight)
NULL
> employees
name age height child smoking
1 Ram 26 5.7 TRUE yes
2 Raj 28 5.9 FALSE no
3 Rajesh 30 5.6 TRUE yes
4 Raheem 35 5.5 FALSE yes
5 Rahul 32 5.8 TRUE no
> cbind(employees, weight)
name age height child smoking weight
1 Ram 26 5.7 TRUE yes 50
2 Raj 28 5.9 FALSE no 60
3 Rajesh 30 5.6 TRUE yes 70
4 Raheem 35 5.5 FALSE yes 50
5 Rahul 32 5.8 TRUE no 60
> employees
name age height child smoking
1 Ram 26 5.7 TRUE yes
2 Raj 28 5.9 FALSE no
3 Rajesh 30 5.6 TRUE yes
4 Raheem 35 5.5 FALSE yes
5 Rahul 32 5.8 TRUE no
> employees<-cbind(employees, weight)
> employees
name age height child smoking weight
1 Ram 26 5.7 TRUE yes 50
2 Raj 28 5.9 FALSE no 60
3 Rajesh 30 5.6 TRUE yes 70
4 Raheem 35 5.5 FALSE yes 50
5 Rahul 32 5.8 TRUE no 60
> basha<-c("Basha", 30, 5.8, TRUE, "no", 68)
> rbind(employees, basha)
name age height child smoking weight
1 Ram 26 5.7 TRUE yes 50
2 Raj 28 5.9 FALSE no 60
3 Rajesh 30 5.6 TRUE yes 70
4 Raheem 35 5.5 FALSE yes 50
5 Rahul 32 5.8 TRUE no 60
6 <NA> 30 5.8 TRUE no 68
> rbind(employees, basha)
name age height child smoking weight
1 Ram 26 5.7 TRUE yes 50
2 Raj 28 5.9 FALSE no 60
3 Rajesh 30 5.6 TRUE yes 70
4 Raheem 35 5.5 FALSE yes 50
5 Rahul 32 5.8 TRUE no 60
6 <NA> 30 5.8 TRUE no 68
Warning message:
In `[<-.factor`(`*tmp*`, ri, value = "Basha") :
invalid factor level, NA generated
> rbind(employees, "basha")
name age height child smoking weight
1 Ram 26 5.7 TRUE yes 50
2 Raj 28 5.9 FALSE no 60
3 Rajesh 30 5.6 TRUE yes 70
4 Raheem 35 5.5 FALSE yes 50
5 Rahul 32 5.8 TRUE no 60
6 <NA> <NA> <NA> <NA> basha basha
Warning messages:
1: In `[<-.factor`(`*tmp*`, ri, value = "basha") :
invalid factor level, NA generated
2: In `[<-.factor`(`*tmp*`, ri, value = "basha") :
invalid factor level, NA generated
3: In `[<-.factor`(`*tmp*`, ri, value = "basha") :
invalid factor level, NA generated
4: In `[<-.factor`(`*tmp*`, ri, value = "basha") :
invalid factor level, NA generated
> rbind(employees, basha)
name age height child smoking weight
1 Ram 26 5.7 TRUE yes 50
2 Raj 28 5.9 FALSE no 60
3 Rajesh 30 5.6 TRUE yes 70
4 Raheem 35 5.5 FALSE yes 50
5 Rahul 32 5.8 TRUE no 60
6 <NA> 30 5.8 TRUE no 68
Warning message:
In `[<-.factor`(`*tmp*`, ri, value = "Basha") :
invalid factor level, NA generated
> rbind(employees, basha)
name age height child smoking weight
1 Ram 26 5.7 TRUE yes 50
2 Raj 28 5.9 FALSE no 60
3 Rajesh 30 5.6 TRUE yes 70
4 Raheem 35 5.5 FALSE yes 50
5 Rahul 32 5.8 TRUE no 60
6 <NA> 30 5.8 TRUE no 68
Warning message:
In `[<-.factor`(`*tmp*`, ri, value = "Basha") :
invalid factor level, NA generated
> employees
name age height child smoking weight
1 Ram 26 5.7 TRUE yes 50
2 Raj 28 5.9 FALSE no 60
3 Rajesh 30 5.6 TRUE yes 70
4 Raheem 35 5.5 FALSE yes 50
5 Rahul 32 5.8 TRUE no 60
> sort(employees$age)
[1] 26 28 30 32 35
Levels: 26 28 30 32 35
> sort[employees$age]
Error in sort[employees$age] :
object of type 'closure' is not subsettable
> sort[(employees$age)]
Error in sort[(employees$age)] :
object of type 'closure' is not subsettable
> sort(employees$age)
[1] 26 28 30 32 35
Levels: 26 28 30 32 35
> ranks
Error: object 'ranks' not found
> ranks<- order(employees$age)
> ranks
[1] 1 2 3 5 4
> employees
name age height child smoking weight
1 Ram 26 5.7 TRUE yes 50
2 Raj 28 5.9 FALSE no 60
3 Rajesh 30 5.6 TRUE yes 70
4 Raheem 35 5.5 FALSE yes 50
5 Rahul 32 5.8 TRUE no 60
> employees[ranks,]
name age height child smoking weight
1 Ram 26 5.7 TRUE yes 50
2 Raj 28 5.9 FALSE no 60
3 Rajesh 30 5.6 TRUE yes 70
5 Rahul 32 5.8 TRUE no 60
4 Raheem 35 5.5 FALSE yes 50
> employees[order(employees$age, decreasing = TRUE),]
name age height child smoking weight
4 Raheem 35 5.5 FALSE yes 50
5 Rahul 32 5.8 TRUE no 60
3 Rajesh 30 5.6 TRUE yes 70
2 Raj 28 5.9 FALSE no 60
1 Ram 26 5.7 TRUE yes 50

You might also like