2016-11-17 73 views
0

我有300个变量(列)以10个时间点(行),在任何给定时间点对各变量I具有温度值A和F.所以在我的数据帧我有2行为时间点1为A + F等如何执行数据的多个子集在for循环中

这是我的再现的例子

Timepoint <- c('1','1','2','2','3','3') 
Temperature <- c('A','F','A','F','A','F') 
Cholesterol <- c('1','2','3','2','3','4') 

data.frame(Timepoint,Temperature,Cholesterol) 

这是我的实际数据

structure(list(Timepoint = c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 
    5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 13L, 13L, 25L, 25L), 
    Temperature = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
    1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("A", 
    "F"), class = "factor"), Concentration.of.chylomicrons = c(1.29e-11, 
    1.25e-11, 1.02e-11, 1.1e-11, 1.08e-11, 1.3e-11, 1.28e-11, 
    1.26e-11, 1.06e-11, 1.32e-11, 8.85e-12, 1.21e-11, 8.83e-12, 
    1.08e-11, 1.35e-11, 1.12e-11, 8.99e-12, 1.08e-11, 9.55e-12, 
    1.04e-11, 0, 1.01e-11), Total.lipids = c(0.00268, 0.0026, 
    0.00208, 0.00225, 0.00222, 0.0027, 0.00268, 0.0026, 0.00219, 
    0.00273, 0.0018, 0.00247, 0.00179, 0.00221, 0.00276, 0.00229, 
    0.00182, 0.00222, 0.00195, 0.00212, 0, 0.00204), Phospholipids = c(0.000224, 
    0.000223, 0.000145, 0.00016, 0.000157, 0.000211, 0.00023, 
    0.000211, 0.000165, 0.000224, 0.000109, 0.00018, 0.000113, 
    0.000163, 0.000175, 0.000177, 0.000122, 0.000173, 0.000127, 
    0.000156, 0, 0.000138)), .Names = c("Timepoint", "Temperature", 
"Concentration.of.chylomicrons", "Total.lipids", "Phospholipids" 
), class = "data.frame", row.names = c(NA, -22L)) 

的样品,我想画一条线图表t o显示每个变量如何随时间变化。在这条线图,我想在A线和B线要绘制

我设法与下列R-代码

plot(DATA$Timepoint[DATA$Temperature=="F"],DATA$variable[DATA$Temperature=="F"]) 

lines(DATA$Timepoint[DATA$Temperature=="F"],DATA$Cvariable[TempMline$Temperature=="A"]) 

这使得地块做这个变量如何涂改随着时间的推移,一个独立的线对于每个温度A + F.

我试图写一个循环命令来绘制每个变量的曲线图,虽然我在与使用[]到子集数据,并且还写循环命令

麻烦

这是我的尝试

lnames <- names(DATA) 
classes<-sapply(DATA,class) 

for(name in lnames[classes == 'numeric']) { 

plot(DATA$Timepoint[DATA$Temperature=="F"],DATA$[,name][DATA$Temperature=="F"]) 
lines(DATA$Timepoint[DATA$Temperature=="F"],DATA$[,name][TempMline$Temperature=="A"]) } 
+1

请阅读本,并上传重复的例子。我http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example –

+0

@ M.Siwik如何上传我的数据帧? – NLM09

+0

在控制台和副本只是dput(数据帧的名字)/粘贴在这里。 –

回答

1

这是你在找什么?我只显示最后一个图,因为这是循环的结束。如果您使用的是RStudio,则只需在曲线查看器中切换左/右箭头键即可。

enter image description here

下面是绘图和循环的代码:

# subset based on temperatures A and F 
a_df <- subset(df, Temperature == "A") 
f_df <- subset(df, Temperature == "F") 

# loop from columns 3:5 (or for the number of variables you have) 
for (i in 3:5) { 
     plot(a_df[, 1], 
      a_df[, i], 
      type = "l", 
      ylab = colnames(a_df[i]), 
      xlab = "Timepoint", 
      lwd = 2, 
      col = "blue") 
     lines(f_df[, 1], 
      f_df[, i], 
      type = "l", 
      lwd = 2, 
      col = "red") 
     legend("bottomleft", 
      col = c("blue", "red"), 
      legend = c("Temperature A", "Temperature F"), 
      lwd = 2, 
      y.intersp = 0.5, 
      bty = "n") 
} 
+0

单词无法表达我的感激之情。非常感谢您的热心帮助图形上的 – NLM09

+0

快问某些数据点在绘图区之外。如何调整图表的比例以确保绘制所有点? – NLM09

+0

对不起,迟到的回应。我相信它是'xlim = c(x,y)和ylim = c(x,y)'。 'xlim'设置你的X轴可以使用的范围。 –