0% found this document useful (0 votes)
16 views3 pages

Expt 2

Uploaded by

Khushi Kumari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views3 pages

Expt 2

Uploaded by

Khushi Kumari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

5/8/24, 4:18 PM Implementation_and_use_of_data_frames_in_R.

ipynb - Colab

creating a dataframe

name <- c("Anne","pete","Frank","Julie","Cath")


age <- c(28,30,21,39,35)
child <- c(FALSE,TRUE,TRUE,FALSE,TRUE)
people <- data.frame(name,age,child,stringsAsFactors = FALSE)

people

A data.frame: 5 × 3
name age child

<chr> <dbl> <lgl>

Anne 28 FALSE

pete 30 TRUE

Frank 21 TRUE

Julie 39 FALSE

Cath 35 TRUE

Select a record - Subset

people[c(3,5),c("name","age","child")]

A data.frame: 2 × 3
name age child

<chr> <dbl> <lgl>

3 Frank 21 TRUE

5 Cath 35 TRUE

people[,"age"]

account_circle 28 · 30 · 21 · 39 · 35

people[1,]

A data.frame: 1 × 3
name age child

<chr> <dbl> <lgl>

1 Anne 28 FALSE

people [1,1]

'Anne'

people [1,"name"]

'Anne'

people[1,"age"]

28

people

https://colab.research.google.com/drive/1tRQtGhxkhSS51uXZM9bptrlVhNgwKzgZ#printMode=true 1/3
5/8/24, 4:18 PM Implementation_and_use_of_data_frames_in_R.ipynb - Colab

A data.frame: 5 × 3
name age child

<chr> <dbl> <lgl>

Anne 28 FALSE

pete 30 TRUE

Frank 21 TRUE

Julie 39 FALSE

Cath 35 TRUE

Extend Add column

# This is formatted as code

height <- c(163,177,163,162,175)


weight <- c(62,54,57,61,58)

people$height <- height

people

A data.frame: 5 × 4
name age child height

<chr> <dbl> <lgl> <dbl>

Anne 28 FALSE 163

pete 30 TRUE 177

Frank 21 TRUE 163

Julie 39 FALSE 162

Cath 35 TRUE 175

people[["weight"]] <- weight

people

A data.frame: 5 × 5
name age child height weight

<chr> <dbl> <lgl> <dbl> <dbl>

Anne 28 FALSE 163 62

pete 30 TRUE 177 54

Frank 21 TRUE 163 57

Julie 39 FALSE 162 61

Cath 35 TRUE 175 58

address <- c("goa","maharashtra","goa","HP","UP")


cbind(people,address)

A data.frame: 5 × 6
name age child height weight address

<chr> <dbl> <lgl> <dbl> <dbl> <chr>

Anne 28 FALSE 163 62 goa

pete 30 TRUE 177 54 maharashtra

Frank 21 TRUE 163 57 goa

Julie 39 FALSE 162 61 HP

Cath 35 TRUE 175 58 UP

Extend Add Row

https://colab.research.google.com/drive/1tRQtGhxkhSS51uXZM9bptrlVhNgwKzgZ#printMode=true 2/3
5/8/24, 4:18 PM Implementation_and_use_of_data_frames_in_R.ipynb - Colab

sam <- data.frame(name="sam", age = 32, child = FALSE, height = 180, weight = 80, address = "ponda")
sam

A data.frame: 1 × 6
name age child height weight address

<chr> <dbl> <lgl> <dbl> <dbl> <chr>

sam 32 FALSE 180 80 ponda

rbind(people,sam)

Error in rbind(deparse.level, ...): numbers of columns of arguments do not match


Traceback:

1. rbind(people, sam)
2. rbind(deparse.level, ...)
3. stop("numbers of columns of arguments do not match")

Sorting by age

ranks <- order(people$age)


ranks
age

3·1·2·5·4
28 · 30 · 21 · 39 · 35

people[ranks,]

A data.frame: 5 × 5
name age child height weight

<chr> <dbl> <lgl> <dbl> <dbl>

3 Frank 21 TRUE 163 57

1 Anne 28 FALSE 163 62

2 pete 30 TRUE 177 54

5 Cath 35 TRUE 175 58

4 Julie 39 FALSE 162 61

sort by height

Start coding or generate with AI.

ranks <- order(people$height, decreasing = TRUE)


ranks
height
people[ranks,]

2·5·1·3·4
163 · 177 · 163 · 162 · 175
A data.frame: 5 × 5
name age child height weight

<chr> <dbl> <lgl> <dbl> <dbl>

2 pete 30 TRUE 177 54

5 Cath 35 TRUE 175 58

1 Anne 28 FALSE 163 62

3 Frank 21 TRUE 163 57

4 Julie 39 FALSE 162 61

https://colab.research.google.com/drive/1tRQtGhxkhSS51uXZM9bptrlVhNgwKzgZ#printMode=true 3/3

You might also like