You are here

Group Exercise: Data Structures

Write R code to create the following:
a) a scalar
b) a vector
c) a matrix
d) a data frame

Paste your R code as a comment to this page.

Comments

> x<-2
> x<-c(2,4,6,8)
> y<-c(1:4)
> z<-cbind(x,y)
> ABC2010<-read.csv("http://www.bcsociology.org/online/files/ABC2010r.csv")

> x<-6
> x
[1] 6
> y<-c(2,5,4,8,6,4)
> y
[1] 2 5 4 8 6 4
> t<-c(3,6,8,4,1,2)
> k<-c(5,1,7,2,9,3)
> z<-rbind(t,k)
> z2<-cbind(t,k)
> z
[,1] [,2] [,3] [,4] [,5] [,6]
t 3 6 8 4 1 2
k 5 1 7 2 9 3
> z2
t k
[1,] 3 5
[2,] 6 1
[3,] 8 7
[4,] 4 2
[5,] 1 9
[6,] 2 3
> AAA<-read.csv("http://www.bcsociology.org/online/files/ABC2010r.csv")

a) scalar --- x<-5
b)vector-----x<-c(1,12,7,8,17)
c) matrix ----a<-matrix(1:15, nrow=3, ncol=5)
d) data frame----df<- data.frame(numbers=c(5,6,7,8,9),text=c("a","b","c","d","a"))

x<-7
> f<-c(1,3,5,7,9,11,13,15)
> m<-matrix(f,nrow=2,ncol=4)
> m
[,1] [,2] [,3] [,4]
[1,] 1 5 9 13
[2,] 3 7 11 15
> View(m)
> df <- data.frame(numbers=c(10,20,30,40),text=c("a","b","c","a"))
> ABC2010
Error: object 'ABC2010' not found
> ABC2010<-read.csv("http://www.bcsociology.org/online/files/abc2010r.csv")
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open URL 'http://www.bcsociology.org/online/files/abc2010r.csv': HTTP status was '404 Not Found'
> ABC2010<-read.csv("http://www.bcsociology.org/online/files/ABC2010r.csv")

> assign("a", 14)
> a+14
[1] 28
> a=14
> a<-c(2,5,7,8)
> library(gmodels)
> ANES2016<-read.csv("http://www.shortell.nyc/online/files/anes_timeseries_2016.csv")
> attach(ANES2016)
> summary(Q2_3)
Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
1.000 1.000 3.000 2.675 4.000 4.000 509
> able(Q2_3)
Error in able(Q2_3) : could not find function "able"
> table(Q2_3)
Q2_3
1 2 3 4
126 109 60 200
> x->3
Error in 3 <- x : invalid (do_set) left-hand side to assignment
> x<-3
> a<-3
> z<-matrix(1:10, nrow=5, ncol=2)
> z
[,1] [,2]
[1,] 1 6
[2,] 2 7
[3,] 3 8
[4,] 4 9
[5,] 5 10
>

> x<1
> x<-c(1,3,7,9)
> y<-c(1:9)
> q<-cbind(x,y)
> > ANES2016<-read.csv("http://www.shortell.nyc/online/files/anes_timeseries_2016.csv")

df <- data.frame(numbers=c(4,10,16,22),text=c("a","b","c","a"))