代码之家  ›  专栏  ›  技术社区  ›  Zaz Volodymyr Null

R高度图-相对于出生日期

  •  0
  • Zaz Volodymyr Null  · 技术社区  · 14 年前

    我有一群人,每个人都有一个空格分隔的文本文件。在这些文件中,右边的值表示该人的身高 cm %d/%m/%Y 格式:

    09/05/1992 0
    17/03/1993 50
    02/08/1994 65.5
    03/12/1995 72
    

    高度 0 标记此人的出生日期。

    pdf("Heights.pdf")
    
    john <- read.table("John",sep="")
    names(john) <- c("time","height")
    jt <- strptime(john$time, "%d/%m/%Y")
    jh <- john$height
    
    amy <- read.table("Amy",sep="")
    names(amy) <- c("time","height")
    at <- strptime(amy$time, "%d/%m/%Y")
    ah <- amy$height
    
    plot(jt,jh,type="b",pch=20,col="red",
    xlab="Date",ylab="Height",
    ylim=c(min(jh,ah),max(jh,ah)))
    points(at,ah,type="b",pch=20,col="green")
    title("Heights")
    

    • 用图表显示当前目录中以结尾的所有文件 .heights ?
    • 把这个图表和每个人的出生日期联系起来?
    1 回复  |  直到 9 年前
        1
  •  2
  •   kmm    14 年前

    # Get all the files ending with .heights
    filelist <- list.files(pattern = "\\.heights")
    
    # Get all the data. Put into a single data.frame
    # Assuming that you don't have thousands of
    # files/measurements, rbind()ing shouldn't be too slow. 
    df <- data.frame(person = character(),
                     dates = character(),
                     height = numeric())
    
    # Iterate through, collecting the data into a data.frame
    for (fname in filelist){
      x <- read.table(fname, sep="", as.is = TRUE)
      person <- gsub("\\.heights", "", fname)
      names(x) <- c("dates", "height")
      df <- rbind(df, data.frame(person = rep(person, times = nrow(x)),
                                 dates = x$dates, 
                                 height = x$height))
    }
    
    # Convert dates to POSIXct
    df$dates <- strptime(as.character(df$dates), "%d/%m/%Y")
    df$dates <- as.POSIXct(df$dates)
    
    # Plot with qplot
    require(ggplot2)
    pdf("Heights.pdf")
    qplot(dates, height, data = df, color = person)
    dev.off()
    
    # Plot with base graphics
    pdf("Heights_2.pdf")
    plot(df$dates, df$height, col = as.numeric(df$person))
    dev.off()