You are on page 1of 2

R Console Page 1

R version 3.4.1 (2017-06-30) -- "Single Candle"


Copyright (C) 2017 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.


You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

Natural language support but running in an English locale

R is a collaborative project with many contributors.


Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or


'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

[Previously saved workspace restored]

> d <- data.frame(age=rnorm(1000,50,12),gender=sample(1:2,1000,repl=T))


> str(d)
'data.frame': 1000 obs. of 2 variables:
$ age : num 65.7 67.6 52.6 57.2 68.4 ...
$ gender: int 2 2 1 1 1 2 2 2 2 2 ...
> d$gender <- as.factor(d$gender)
> d$income <- rt(1000,23) + 5.5
> str(d)
'data.frame': 1000 obs. of 3 variables:
$ age : num 65.7 67.6 52.6 57.2 68.4 ...
$ gender: Factor w/ 2 levels "1","2": 2 2 1 1 1 2 2 2 2 2 ...
$ income: num 5.6 5.19 4.78 5.45 6.31 ...
> aggregate(d$age, by=list(d$gender,sd)
+ aggregate(d$age, by=list(d$gender,sd))
Error: unexpected symbol in:
"aggregate(d$age, by=list(d$gender,sd)
aggregate"
> aggregate(d$age, by=list(d$gender,sd))
Error in match.fun(FUN) : argument "FUN" is missing, with no default
> aggregate(d$age, by=list(d$gender),sd)
Group.1 x
1 1 12.63383
2 2 11.67396
> d$occu <- sample(as.factor(d$occu) -> integer
+ d$occu <- sample(as.factor(d$occu)
Error: unexpected symbol in:
"d$occu <- sample(as.factor(d$occu) -> integer
d"
> d$occu <- sample(as.factor(d$occu)
+
+ 0
Error: unexpected numeric constant in:
"
0"
> d$occu <- sample(as.factor(d$occu))
Error in `$<-.data.frame`(`*tmp*`, occu, value = integer(0)) :
replacement has 0 rows, data has 1000
> d$occu <- sample(1:4,1000,repl=T)
> d$occu <- as.factor(d$occu)
> aggregate(d$age, by=list(d$gender, d$occu), mean)
Group.1 Group.2 x
1 1 1 51.12679
2 2 1 49.81537
3 1 2 50.43417
4 2 2 52.24305
5 1 3 49.79583
6 2 3 49.69839
7 1 4 50.14357
8 2 4 51.48889
R Console Page 2

> aggregate(cbind(d$age, d$inc), by=list(d$gender, d$occu), mean) -> t


> str(t)
'data.frame': 8 obs. of 4 variables:
$ Group.1: Factor w/ 2 levels "1","2": 1 2 1 2 1 2 1 2
$ Group.2: Factor w/ 4 levels "1","2","3","4": 1 1 2 2 3 3 4 4
$ V1 : num 51.1 49.8 50.4 52.2 49.8 ...
$ V2 : num 5.5 5.5 5.52 5.39 5.58 ...
> View(t)
> colnames(t) <- c('gender', 'occu', 'mean.age', 'mean.inc')
> View(t)
> colnames(t)[2] <- 'occupation'
> edit(t) -> t
> cut(d$inc,3,c('low', 'medium', 'high')) -> d$incgrp
> View(d)
> table(d$incgrp)

low medium high


60 742 198
> S= "21-09-2016"
> S= "21-09-2017"
> S1 = "21/Sep/17"
> sdate <- as.Date(S, "%d-%m-%Y")
> s1date <- as.Date(S1, "%d/%b/%y")
> sdate
[1] "2017-09-21"
> s1date
[1] "2017-09-21"
> S1date <- as.Date(S1, "%d/%b/%y")
> S1date
[1] "2017-09-21"
> sdate-S1date
Time difference of 0 days
> read.csv(file.choose(); header=T) -> d
Error: unexpected ';' in "read.csv(file.choose();"
>

You might also like