KyungHwan's etc.

R 기본 문법 연습 1 본문

R

R 기본 문법 연습 1

KyungHwan_0 2018. 10. 14. 20:22

R 기본 문법 연습

x<-3
print(x)
x<-c("fee","fie","foe","fum")
print(x)
c(1,1,2,3,5,8,13,21)
c(1*pi,2*pi,3*pi,4*pi)
c("Everyone","loves","stats.")
c(TRUE,TRUE,FALSE,TRUE)

v1<-c(1,2,3)
v2<-c(4,5,6)
v3<-c("A","B","C")
c(v1,v3)

1:5
10:19
9:0

seq(from=0,to=20,by=2)
seq(from=0,to=20,length.out=5)
seq(from=1.0,to=2.0,length.out=5)
rep(1,times=5)
rep(1:2,each=2)
a<-3
a==pi
a!=pi
a<pi
a>pi
a<=pi
a>=pi
fib<-c(0,1,1,2,3,5,8,13,21,34)
fib
fib[1]
fib[3]
fib[1:3]
fib[c(1,2,4,8)]
fib[-1]
fib[-c(1:3)]
fib<10
fib[fib<10]
fib%%2==0
fib[fib%%2==0]

years<-c(1960,1964,1976,1994)
names(years)<-c("Kennedy","Johnson","Carter","Clinton")
years
years["Carter"]
years["Clinton"]

x<-c(0,1,1,2,3,5,8,13,21,34)
mean(x)
median(x)
sd(x)
var(x)
x<-c(0,1,1,2,3,5,8,13,21,34)
y<-log(x+1)
y
cor(x,y)
cov(x,y)

theData<-c(1.1,1.2,2.1,2.2,3.1,3.2)
mat<-matrix(theData,2,3)
mat
dim(mat)
t(mat)  #mat
mat%*%t(mat) #mat
diag(mat)  #n
colnames(mat)<-c("IBM","MSFT","GOOG")
rownames(mat)<-c("IBM","MSFT")
mat
mat[1,]  #ù° ??
mat[,3]  #??° ??
lst<-list(3.14,"Moe",c(1,1,2,3),mean)
lst

a=c(1,2,4,6,3,4)
b=c(6,4,2,4,3,4)
c=c(7,6,4,2,5,6)
d=c(2,4,3,1,5,6)
e=data.frame(a,b,c,d)
e

b=c("Mr.Foo", "Ms. Bar", "Mr. Baz")
c=c(95, 97, 92)
d=data.frame(b, c)
d
colnames(d)<-c("name", "score")
d
setwd("c:/r_working")
getwd()
write.csv(d,file="a.csv")
read.csv("a.csv", header = TRUE)
x<-read.csv("a.csv")
x
colnames(x)<-c("id", "name", "score")
x

R Studio 로 확인하면서 기본적인 문법을 공부할수있다.

'R' 카테고리의 다른 글

R 웹 크롤링 or 워드클라우드  (0) 2018.10.17
R을 이용한 차트 그리기  (0) 2018.10.16
R 기본 데이터형  (0) 2018.10.14
R 기본 문법 연습 3  (0) 2018.10.14
R 기본 문법 연습 2  (0) 2018.10.14
Comments