ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • R program(8) Data
    Cording/R program 2021. 1. 11. 21:23

    rm(list=Is())

     

    setwd("c:/data")

    write.csv(iris,"a.csv")

    df <- read.csv("a.csv")

    str(df)

    df <- read.csv("a.csv", header=F)

    str(df)

    df <- read.csv("a.csv", header=F, stringsAsFactors=T)

    str(df)

     

    # subset: 데이터 추출

    sub_df <- subset(df,subset=(Sepal.Length<5.5))

    sub2_df <- subset(df,subset=(Sepal.Length<5.5 & Species=="setosa"))

     

    sub3_df <- subset(df,select=-Sepal.Length)

     

    # 데이터 병합

    # 행 병합

    r_df <- rbind(sub_df,sub2_df)

    r_df

     

    # 열로 병합

    sub_df <- df[,1:3]

    sub2_df <- df[,4:6]

     

    c_df <- cbind(sub_df,sub2_df)

     

    # 변수명 변경

    colnames(c_df)

    colnames(c_df) <- c("a","b",3,4,5,6)

    str(c_df)

     

    # merge: 데이터 병합

    x <- data.frame(name=c("a","b","c"),a=c(1,2,3))

    x

    y <- data.frame(name=c("a","b","c"),b=c(4,5,6))

    y

     

    z <- merge(x,y, by="name")

    z

     

    ·install.packages("dplyr")

    library(dplyr)

    df <- iris

    str(df)

    head(df)

     

    # filter: 행 선택

    sub_df <- df %>% filter(Species=="setosa")

    head(sub_df)

    sub2_df <- df %>% filter(Species=="setosa" & Sepal.Length > 5)

    head(sub2_df)

     

    # select: (변수) 선택

    sub3_df <- df %>% select(c(Sepal.Length,Sepal.width))

    str(sub3_df)

    str(df)

     

    # f, s 조합

    sub3_df <- df %>% filter(Species=="setosa") %>% select(c(Sepal.Length,Sepal.Width))

    str(sub5_df)

     

    # arrange: 정렬

    sort_df <- df %>% arrange(Sepal.Length)

    head(sort_df)

    sort_df <- df %>% arrange(desc(Sepal.Length))

    head(sort_df)

     

    # summarise, group_by

     

    df %>% summarise(Sep_leh_mean=mean(Sepal.Length))

    df %>% group_by(Species) %>%

    summarise(Sep_leh_mean=mean(Sepal.Length),

    Sep_wid_mean=mean(Sepal.Width),

    Pet_len_mean=mean(petal.Length),

    Pet_wid_mean=mean(Petal.Width))

     

    # reshape 패키지

    # install.packages("reshape2")

    library(reshape2)

     

    airquality

    head(airquality)

    str(airquality)

    df <- airquality

     

    # melt: 기준으로 재정렬

    melt_df <- melt(df,id=c("Month","Day))

    head(melt_df)

     

    # cast 재형태화

    cast_df <- dcast(melt_df,Day=month+variable)

    cast_df

     

    a <- NA

    is.na(a)

    is.na(1)

    airquality

    is.na(airquality)

    df <- airquality

    na.omit(df)

    complete.cases(df)

    a <- df[complete.cases(df),]

    a

    omit_df <- na.omit(df)

     

    names(mtcars)[1] <- c("MPG")

    names(mtcars)

     

    names(mtcars)[which[names(mtcars) == "cyl")] <- c("CYL")

     

    mtcars <- rename(mtcars, mpg = MPG, cyl = CYL)

     

    names(mtcars)

    str(mtcars)

     

    # 일정 이상

    data()
    library(dplyr)
    data("starwars")
    str(starwars)
    names(starwars)
    starwars %>%
      filter(height > 191) %>%
      select(name, hair_color)

    starwars[starwars$height > 191, c("name", "hair_color")]

    'Cording > R program' 카테고리의 다른 글

    Ongoing  (0) 2023.12.25
    R program(7)  (0) 2020.12.26
    R program(6)  (0) 2020.12.24
    R program(5)  (0) 2020.12.23
    R program(4)  (0) 2020.12.22

    댓글

Designed by Tistory.