Extracting data from *.txt output file into R - r

I have an output.txt file that results from an analysis I am doing in R. I would like to extract:
The << Output >> tables from the .txt file for each subject and combine them into an R data frame. The output column names are consistent between each of the subjects.
The same for the << Predicted Output >> and combine into a data frame in R.
As noted, the output file has written text in between that I don't need.
To make it easy for looking into the structure of the output.txt file, I have uploaded the .txt file on the following link here. I am also putting a screenshot below to show how the output is structured.
I attempted to do this using things like this but no luck:
df <- read.delim("ivivc_outputs.txt").

Try this to get started. Add more conditions if you need. I hope, this is helpful. If you need anything feel free to ask.
b = readLines(file('ivivc_outputs.txt', 'r'))
n_out = 1
n_pred = 1
listOutput = list()
listPredictOutput = list()
for(i in 1:length(b)){
if(b[i] == "<< Output >>"){
a = strsplit(b[i+1], " ")[[1]]
a = a[a != ""]
# print(a)
df <- data.frame(matrix(ncol = length(a), nrow = 0))
colnames(df) = a
control = 2
while(control != 20){
l = strsplit(b[i+control], " ")[[1]]
l = l[l != ""]
df[control-1,] = l
control = control + 1
}
listOutput[[n_out]] = df
n_out = n_out+1
}
if(b[i] == "<< Predicted Output >>"){
a = strsplit(b[i+1], " ")[[1]]
a = a[a != ""]
# print(a)
df <- data.frame(matrix(ncol = length(a), nrow = 0))
colnames(df) = a
control = 2
while(control != 20){
l = strsplit(b[i+control], " ")[[1]]
l = l[l != ""]
df[control-1,] = l
control = control + 1
}
listPredictOutput[[n_pred]] = df
n_pred = n_pred+1
}
}
# to merge all data frames use `bind_rows` from `dplyr`
library(dplyr)
dfOutput = bind_rows(listOutput)
dfPredictOutput = bind_rows(listPredictOutput)
> dfPredictOutput
# pH subj formula. time FABpred conc.pred AUCpred
# 1 1.2 1 capsule 0.0 0.000000 0.00000 0.00000
# 2 1.2 1 capsule 1.0 2.528737 8.39300 4.19650
# 3 1.2 1 capsule 2.0 7.415708 22.57987 19.68293
# 4 1.2 1 capsule 3.0 15.845734 45.08950 53.51761
# 5 1.2 1 capsule 4.0 24.275759 62.14611 107.13542
# 6 1.2 1 capsule 5.0 33.133394 76.48998 176.45346
# 7 1.2 1 capsule 6.0 41.991029 87.35901 258.37796
# 8 1.2 1 capsule 6.5 45.900606 89.90799 302.69471
# 9 1.2 1 capsule 7.0 49.810183 92.12832 348.20378
# 10 1.2 1 capsule 7.5 53.719760 94.06237 394.75146
# 11 1.2 1 capsule 8.0 57.629337 95.74705 442.20381
# 12 1.2 1 capsule 8.0 57.629337 95.74705 442.20381
# 13 1.2 1 capsule 9.0 63.860225 93.23271 536.69369
# 14 1.2 1 capsule 10.0 70.091113 91.32747 628.97378
# 15 1.2 1 capsule 12.0 79.498532 79.70975 800.01101
# 16 1.2 1 capsule 16.0 90.372043 49.52751 1058.48553
# 17 1.2 1 capsule 20.0 101.245554 40.79704 1239.13462
# 18 1.2 1 capsule 24.0 107.354268 26.67212 1374.07293
# 19 1.2 1 capsuleContent 0.0 0.000000 0.000000 0.000000
# 20 1.2 1 capsuleContent 1.0 1.490256 4.946231 2.473115
# 21 1.2 1 capsuleContent 2.0 5.338746 16.521316 13.206889
# 22 1.2 1 capsuleContent 3.0 13.341161 39.079386 41.007240
# 23 1.2 1 capsuleContent 4.0 21.343576 56.172708 88.633287
# 24 1.2 1 capsuleContent 5.0 30.017950 71.355393 152.397337
# 25 1.2 1 capsuleContent 6.0 38.692324 82.860036 229.505052
# 26 1.2 1 capsuleContent 6.5 42.571357 85.881180 271.690356
# 27 1.2 1 capsuleContent 7.0 46.450390 88.512793 315.288850
# 28 1.2 1 capsuleContent 7.5 50.329423 90.805099 360.118323
# 29 1.2 1 capsuleContent 8.0 54.208457 92.801847 406.020060
# 30 1.2 1 capsuleContent 8.0 54.208457 92.801847 406.020060
# 31 1.2 1 capsuleContent 9.0 60.439345 91.000989 497.921478
# 32 1.2 1 capsuleContent 10.0 66.670233 89.636393 588.240168
# 33 1.2 1 capsuleContent 12.0 76.322001 79.472871 757.349432
# 34 1.2 1 capsuleContent 16.0 87.256599 49.607701 1015.510577
# 35 1.2 1 capsuleContent 20.0 98.191197 40.968947 1196.663873
# 36 1.2 1 capsuleContent 24.0 104.177736 26.424419 1331.450604
# 37 1.2 2 capsule 0.0 0.000000 0.00000 0.00000
# 38 1.2 2 capsule 1.0 2.528737 8.39300 4.19650
# 39 1.2 2 capsule 2.0 7.415708 22.57987 19.68293
# 40 1.2 2 capsule 3.0 15.845734 45.08950 53.51761
# 41 1.2 2 capsule 4.0 24.275759 62.14611 107.13542
# 42 1.2 2 capsule 5.0 33.133394 76.48998 176.45346
# 43 1.2 2 capsule 6.0 41.991029 87.35901 258.37796
# 44 1.2 2 capsule 6.5 45.900606 89.90799 302.69471
# 45 1.2 2 capsule 7.0 49.810183 92.12832 348.20378
# 46 1.2 2 capsule 7.5 53.719760 94.06237 394.75146
# 47 1.2 2 capsule 8.0 57.629337 95.74705 442.20381
# 48 1.2 2 capsule 8.0 57.629337 95.74705 442.20381
# 49 1.2 2 capsule 9.0 63.860225 93.23271 536.69369
# 50 1.2 2 capsule 10.0 70.091113 91.32747 628.97378
# 51 1.2 2 capsule 12.0 79.498532 79.70975 800.01101
# 52 1.2 2 capsule 16.0 90.372043 49.52751 1058.48553
# 53 1.2 2 capsule 20.0 101.245554 40.79704 1239.13462
# 54 1.2 2 capsule 24.0 107.354268 26.67212 1374.07293
# 55 1.2 2 capsuleContent 0.0 0.000000 0.000000 0.000000
# 56 1.2 2 capsuleContent 1.0 1.490256 4.946231 2.473115
# 57 1.2 2 capsuleContent 2.0 5.338746 16.521316 13.206889
# 58 1.2 2 capsuleContent 3.0 13.341161 39.079386 41.007240
# 59 1.2 2 capsuleContent 4.0 21.343576 56.172708 88.633287
# 60 1.2 2 capsuleContent 5.0 30.017950 71.355393 152.397337
# 61 1.2 2 capsuleContent 6.0 38.692324 82.860036 229.505052
# 62 1.2 2 capsuleContent 6.5 42.571357 85.881180 271.690356
# 63 1.2 2 capsuleContent 7.0 46.450390 88.512793 315.288850
# 64 1.2 2 capsuleContent 7.5 50.329423 90.805099 360.118323
# 65 1.2 2 capsuleContent 8.0 54.208457 92.801847 406.020060
# 66 1.2 2 capsuleContent 8.0 54.208457 92.801847 406.020060
# 67 1.2 2 capsuleContent 9.0 60.439345 91.000989 497.921478
# 68 1.2 2 capsuleContent 10.0 66.670233 89.636393 588.240168
# 69 1.2 2 capsuleContent 12.0 76.322001 79.472871 757.349432
# 70 1.2 2 capsuleContent 16.0 87.256599 49.607701 1015.510577
# 71 1.2 2 capsuleContent 20.0 98.191197 40.968947 1196.663873
# 72 1.2 2 capsuleContent 24.0 104.177736 26.424419 1331.450604
# 73 1.2 3 capsule 0.0 0.000000 0.00000 0.00000
# 74 1.2 3 capsule 1.0 2.528737 8.39300 4.19650
# 75 1.2 3 capsule 2.0 7.415708 22.57987 19.68293
# 76 1.2 3 capsule 3.0 15.845734 45.08950 53.51761
# 77 1.2 3 capsule 4.0 24.275759 62.14611 107.13542
# 78 1.2 3 capsule 5.0 33.133394 76.48998 176.45346
# 79 1.2 3 capsule 6.0 41.991029 87.35901 258.37796
# 80 1.2 3 capsule 6.5 45.900606 89.90799 302.69471
# 81 1.2 3 capsule 7.0 49.810183 92.12832 348.20378
# 82 1.2 3 capsule 7.5 53.719760 94.06237 394.75146
# 83 1.2 3 capsule 8.0 57.629337 95.74705 442.20381
# 84 1.2 3 capsule 8.0 57.629337 95.74705 442.20381
# 85 1.2 3 capsule 9.0 63.860225 93.23271 536.69369
# 86 1.2 3 capsule 10.0 70.091113 91.32747 628.97378
# 87 1.2 3 capsule 12.0 79.498532 79.70975 800.01101
# 88 1.2 3 capsule 16.0 90.372043 49.52751 1058.48553
# 89 1.2 3 capsule 20.0 101.245554 40.79704 1239.13462
# 90 1.2 3 capsule 24.0 107.354268 26.67212 1374.07293
# 91 1.2 3 capsuleContent 0.0 0.000000 0.000000 0.000000
# 92 1.2 3 capsuleContent 1.0 1.490256 4.946231 2.473115
# 93 1.2 3 capsuleContent 2.0 5.338746 16.521316 13.206889
# 94 1.2 3 capsuleContent 3.0 13.341161 39.079386 41.007240
# 95 1.2 3 capsuleContent 4.0 21.343576 56.172708 88.633287
# 96 1.2 3 capsuleContent 5.0 30.017950 71.355393 152.397337
# 97 1.2 3 capsuleContent 6.0 38.692324 82.860036 229.505052
# 98 1.2 3 capsuleContent 6.5 42.571357 85.881180 271.690356
# 99 1.2 3 capsuleContent 7.0 46.450390 88.512793 315.288850
# 100 1.2 3 capsuleContent 7.5 50.329423 90.805099 360.118323
# 101 1.2 3 capsuleContent 8.0 54.208457 92.801847 406.020060
# 102 1.2 3 capsuleContent 8.0 54.208457 92.801847 406.020060
# 103 1.2 3 capsuleContent 9.0 60.439345 91.000989 497.921478
# 104 1.2 3 capsuleContent 10.0 66.670233 89.636393 588.240168
# 105 1.2 3 capsuleContent 12.0 76.322001 79.472871 757.349432
# 106 1.2 3 capsuleContent 16.0 87.256599 49.607701 1015.510577
# 107 1.2 3 capsuleContent 20.0 98.191197 40.968947 1196.663873
# 108 1.2 3 capsuleContent 24.0 104.177736 26.424419 1331.450604

Related

Wrong Fit using nls function

When I try to fit an exponential decay and my x axis has decimal number, the fit is never correct. Here's my data below:
exp.decay = data.frame(time,counts)
time counts
1 0.4 4458
2 0.6 2446
3 0.8 1327
4 1.0 814
5 1.2 549
6 1.4 401
7 1.6 266
8 1.8 182
9 2.0 140
10 2.2 109
11 2.4 83
12 2.6 78
13 2.8 57
14 3.0 50
15 3.2 31
16 3.4 22
17 3.6 23
18 3.8 20
19 4.0 19
20 4.2 9
21 4.4 7
22 4.6 4
23 4.8 6
24 5.0 4
25 5.2 6
26 5.4 2
27 5.6 7
28 5.8 2
29 6.0 0
30 6.2 3
31 6.4 1
32 6.6 1
33 6.8 2
34 7.0 1
35 7.2 2
36 7.4 1
37 7.6 1
38 7.8 0
39 8.0 0
40 8.2 0
41 8.4 0
42 8.6 1
43 8.8 0
44 9.0 0
45 9.2 0
46 9.4 1
47 9.6 0
48 9.8 0
49 10.0 1
fit.one.exp <- nls(counts ~ A*exp(-k*time),data=exp.decay, start=c(A=max(counts),k=0.1))
plot(exp.decay, col='darkblue',xlab = 'Track Duration (seconds)',ylab = 'Number of Particles', main = 'Exponential Fit')
lines(predict(fit.one.exp), col = 'red', lty=2, lwd=2)
I always get this weird fit. Seems to me that the fit is not recognizing the right x axis, because when I use a different set of data, with only integers in the x axis (time) the fit works! I don't understand why it's different with different units.
You need one small modification:
lines(predict(fit.one.exp), col = 'red', lty=2, lwd=2)
should be
lines(exp.decay$time, predict(fit.one.exp), col = 'red', lty=2, lwd=2)
This way you make sure to plot against the desired values on your abscissa.
I tested it like this:
data = read.csv('exp_fit_r.csv')
A0 <- max(data$count)
k0 <- 0.1
fit <- nls(data$count ~ A*exp(-k*data$time), start=list(A=A0, k=k0), data=data)
plot(data)
lines(data$time, predict(fit), col='red')
which gives me the following output:
As you can see, the fit describes the actual data very well, it was just a matter of plotting against the correct abscissa values.

Does ggplot2 exclude some data?

I want to create some basic grouped barplots with ggplot2 but it seems to exclude some data. If I review my input data everything is there, but some bars are missing and it is also messing with the error bars. I tried to convert into multiple variable types, regrouped, loaded again, saved everything in .csv and loaded all new... I just don't know what is wrong.
Here is my code:
library(ggplot2)
limits <- aes(ymax = DataCm$mean + DataCm$sd,
ymin = DataCm$mean - DataCm$sd)
p <- ggplot(data = DataCm, aes(x = factor(DataCm$Zeit), y = factor(DataCm$mean)
) )
p + geom_bar(stat = "identity",
position = position_dodge(0.9),fill =DataCm$group) +
geom_errorbar(limits, position = position_dodge(0.9),
width = 0.25) +
labs(x = "Time [min]", y = "Individuals per foodsource")
This is DataCm:
Zeit mean sd group
1 30 0.1 0.3162278 1
2 60 0.0 0.0000000 2
3 90 0.1 0.3162278 3
4 120 0.0 0.0000000 4
5 150 0.1 0.3162278 5
6 180 0.1 0.3162278 6
7 240 0.3 0.6749486 1
8 300 0.3 0.6749486 2
9 360 0.3 0.6749486 3
10 30 0.1 0.3162278 4
11 60 0.1 0.3162278 5
12 90 0.2 0.4216370 6
13 120 0.3 0.4830459 1
14 150 0.3 0.4830459 2
15 180 0.4 0.5163978 3
16 240 0.3 0.4830459 4
17 300 0.4 0.5163978 5
18 360 0.4 0.5163978 6
19 30 1.2 1.1352924 1
20 60 1.8 1.6865481 2
21 90 2.2 2.0976177 3
22 120 2.2 2.0976177 4
23 150 2.0 1.8856181 5
24 180 2.3 1.9465068 6
25 240 2.4 2.0655911 1
26 300 2.1 1.8529256 2
27 360 2.0 2.1602469 3
28 30 0.2 0.4216370 4
29 60 0.1 0.3162278 5
30 90 0.1 0.3162278 6
31 120 0.1 0.3162278 1
32 150 0.0 0.0000000 2
33 180 0.1 0.3162278 3
34 240 0.1 0.3162278 4
35 300 0.1 0.3162278 5
36 360 0.1 0.3162278 6
37 30 1.3 1.5670212 1
38 60 1.5 1.5811388 2
39 90 1.5 1.7159384 3
40 120 1.5 1.9002924 4
41 150 1.9 2.1317703 5
42 180 1.9 2.1317703 6
43 240 2.2 2.3475756 1
44 300 2.4 2.3190036 2
45 360 2.2 2.1499354 3
46 30 2.1 2.1317703 4
47 60 3.0 2.2110832 5
48 90 3.3 2.1628171 6
49 120 3.2 2.1499354 1
50 150 3.4 2.6331224 2
51 180 3.5 2.4152295 3
52 240 3.7 2.6267851 4
53 300 3.7 2.4060110 5
54 360 3.8 2.6583203 6
The output is:
Maybe you can help me. Thanks in advance!
Best wishes,
Benjamin
Solved it:
I reshaped everything in Excel and exported it another way. The group variable was also not the way I wanted it. Now it is fixed, but I can't really tell you why.
Your data looks malformed. I guess you wanted to have 6 different group values for each time point, but now the group variable just loops over, and you have:
1 30 0.1 0.3162278 1
...
10 30 0.1 0.3162278 4
...
19 30 1.2 1.1352924 1
...
28 30 0.2 0.4216370 4
geom_bar then probably omits rows that have identical mean and time. Although I am not sure why it chooses to do so, you should solve the group problem first anyway.

Calculating tree growth using for loop

I am trying to calculate diameter growth for a set of trees over a number of years in a dataframe in which each row is a given tree during a given year. Typically, this sort of data has each individual stem as a single row with that stem's diameter for each year given in a separate column, but for various reasons, this dataframe needs to remain such that each row is an individual stem in an individual year. A simplistic model version of the data would be as follows
df<-data.frame("Stem"=c(1:5,1:5,1,2,3,5,1,2,3,5,6),
"Year"=c(rep(1997,5), rep(1998,5), rep(1999,4), rep(2000,5)),
"Diameter"=c(1:5,seq(1.5,5.5,1),2,3,4,6,3,5,7,9,15))
df
Stem Year DAP
1 1 1997 1.0
2 2 1997 2.0
3 3 1997 3.0
4 4 1997 4.0
5 5 1997 5.0
6 1 1998 1.5
7 2 1998 2.5
8 3 1998 3.5
9 4 1998 4.5
10 5 1998 5.5
11 1 1999 2.0
12 2 1999 3.0
13 3 1999 4.0
14 5 1999 6.0
15 1 2000 3.0
16 2 2000 5.0
17 3 2000 7.0
18 5 2000 9.0
19 6 2000 15.0
What I am trying to accomplish is to make a new column that takes the diameter for a given stem in a given year and subtracts the diameter for that same stem in the previous year. I assume that this will require some set of nested for loops. Something like
for (i in 1:length(unique(df$Stem_ID){
for (t in 2:length(unique(df$Year){
.....
}
}
What I'm struggling with is how to write the function that calculates:
Diameter[t]-Diameter[t-1] for each stem. Any suggestions would be greatly appreciated.
Try:
> do.call(rbind, lapply(split(df, df$Stem), function(x) transform(x, diff = c(0,diff(x$Diameter)))))
Stem Year Diameter diff
1.1 1 1997 1.0 0.0
1.6 1 1998 1.5 0.5
1.11 1 1999 2.0 0.5
1.15 1 2000 3.0 1.0
2.2 2 1997 2.0 0.0
2.7 2 1998 2.5 0.5
2.12 2 1999 3.0 0.5
2.16 2 2000 5.0 2.0
3.3 3 1997 3.0 0.0
3.8 3 1998 3.5 0.5
3.13 3 1999 4.0 0.5
3.17 3 2000 7.0 3.0
4.4 4 1997 4.0 0.0
4.9 4 1998 4.5 0.5
5.5 5 1997 5.0 0.0
5.10 5 1998 5.5 0.5
5.14 5 1999 6.0 0.5
5.18 5 2000 9.0 3.0
6 6 2000 15.0 0.0
Rnso's answer works. You could also do the slightly shorter:
>df[order(df$Stem),]
>df$diff <- unlist(tapply(df$Diameter,df$Stem, function(x) c(NA,diff(x))))
Stem Year Diameter diff
1 1 1997 1.0 NA
6 1 1998 1.5 0.5
11 1 1999 2.0 0.5
15 1 2000 3.0 1.0
2 2 1997 2.0 NA
7 2 1998 2.5 0.5
12 2 1999 3.0 0.5
16 2 2000 5.0 2.0
3 3 1997 3.0 NA
8 3 1998 3.5 0.5
13 3 1999 4.0 0.5
17 3 2000 7.0 3.0
4 4 1997 4.0 NA
9 4 1998 4.5 0.5
5 5 1997 5.0 NA
10 5 1998 5.5 0.5
14 5 1999 6.0 0.5
18 5 2000 9.0 3.0
19 6 2000 15.0 NA
Or if you're willing to use the data.table package you can be very succinct:
>require(data.table)
>DT <- data.table(df)
>setkey(DT,Stem)
>DT <- DT[,diff:= c(NA, diff(Diameter)), by = Stem]
>df <- as.data.frame(DT)
Stem Year Diameter diff
1 1 1997 1.0 NA
2 1 1998 1.5 0.5
3 1 1999 2.0 0.5
4 1 2000 3.0 1.0
5 2 1997 2.0 NA
6 2 1998 2.5 0.5
7 2 1999 3.0 0.5
8 2 2000 5.0 2.0
9 3 1997 3.0 NA
10 3 1998 3.5 0.5
11 3 1999 4.0 0.5
12 3 2000 7.0 3.0
13 4 1997 4.0 NA
14 4 1998 4.5 0.5
15 5 1997 5.0 NA
16 5 1998 5.5 0.5
17 5 1999 6.0 0.5
18 5 2000 9.0 3.0
19 6 2000 15.0 NA
If you have a large dataset, data.table has the advantage of being extremely fast.

R ggplot2 legend inside the figure

So, I have the following data.frame, and I want to generate two plots in one graph for yval vs. xval, for each zval and type tp. The lef
> df
xval yval se zval cond
1 1.0 1.831564e-02 1.831564e-03 0 a
2 1.2 2.705185e-02 2.705185e-03 0 a
3 1.4 3.916390e-02 3.916390e-03 0 a
4 1.6 5.557621e-02 5.557621e-03 0 a
5 1.8 7.730474e-02 7.730474e-03 0 a
6 2.0 1.053992e-01 1.053992e-02 0 a
7 2.2 1.408584e-01 1.408584e-02 0 a
8 2.4 1.845195e-01 1.845195e-02 0 a
9 2.6 2.369278e-01 2.369278e-02 0 a
10 2.8 2.981973e-01 2.981973e-02 0 a
11 3.0 3.678794e-01 3.678794e-02 0 a
12 3.2 4.448581e-01 4.448581e-02 0 a
13 3.4 5.272924e-01 5.272924e-02 0 a
14 3.6 6.126264e-01 6.126264e-02 0 a
15 3.8 6.976763e-01 6.976763e-02 0 a
16 4.0 7.788008e-01 7.788008e-02 0 a
17 4.2 8.521438e-01 8.521438e-02 0 a
18 4.4 9.139312e-01 9.139312e-02 0 a
19 4.6 9.607894e-01 9.607894e-02 0 a
20 4.8 9.900498e-01 9.900498e-02 0 a
21 5.0 1.000000e+00 1.000000e-01 0 a
22 5.2 9.900498e-01 9.900498e-02 0 a
23 5.4 9.607894e-01 9.607894e-02 0 a
24 5.6 9.139312e-01 9.139312e-02 0 a
25 5.8 8.521438e-01 8.521438e-02 0 a
26 6.0 7.788008e-01 7.788008e-02 0 a
27 6.2 6.976763e-01 6.976763e-02 0 a
28 6.4 6.126264e-01 6.126264e-02 0 a
29 6.6 5.272924e-01 5.272924e-02 0 a
30 6.8 4.448581e-01 4.448581e-02 0 a
31 7.0 3.678794e-01 3.678794e-02 0 a
32 7.2 2.981973e-01 2.981973e-02 0 a
33 7.4 2.369278e-01 2.369278e-02 0 a
34 7.6 1.845195e-01 1.845195e-02 0 a
35 7.8 1.408584e-01 1.408584e-02 0 a
36 8.0 1.053992e-01 1.053992e-02 0 a
37 8.2 7.730474e-02 7.730474e-03 0 a
38 8.4 5.557621e-02 5.557621e-03 0 a
39 8.6 3.916390e-02 3.916390e-03 0 a
40 8.8 2.705185e-02 2.705185e-03 0 a
41 9.0 1.831564e-02 1.831564e-03 0 a
42 9.2 1.215518e-02 1.215518e-03 0 a
43 9.4 7.907054e-03 7.907054e-04 0 a
44 9.6 5.041760e-03 5.041760e-04 0 a
45 9.8 3.151112e-03 3.151112e-04 0 a
46 10.0 1.930454e-03 1.930454e-04 0 a
47 1.0 3.726653e-06 7.453306e-07 0 b
48 1.2 9.929504e-06 1.985901e-06 0 b
49 1.4 2.541935e-05 5.083869e-06 0 b
50 1.6 6.252150e-05 1.250430e-05 0 b
51 1.8 1.477484e-04 2.954967e-05 0 b
52 2.0 3.354626e-04 6.709253e-05 0 b
53 2.2 7.318024e-04 1.463605e-04 0 b
54 2.4 1.533811e-03 3.067621e-04 0 b
55 2.6 3.088715e-03 6.177431e-04 0 b
56 2.8 5.976023e-03 1.195205e-03 0 b
57 3.0 1.110900e-02 2.221799e-03 0 b
58 3.2 1.984109e-02 3.968219e-03 0 b
59 3.4 3.404745e-02 6.809491e-03 0 b
60 3.6 5.613476e-02 1.122695e-02 0 b
61 3.8 8.892162e-02 1.778432e-02 0 b
62 4.0 1.353353e-01 2.706706e-02 0 b
63 4.2 1.978987e-01 3.957974e-02 0 b
64 4.4 2.780373e-01 5.560746e-02 0 b
65 4.6 3.753111e-01 7.506222e-02 0 b
66 4.8 4.867523e-01 9.735045e-02 0 b
67 5.0 6.065307e-01 1.213061e-01 0 b
68 5.2 7.261490e-01 1.452298e-01 0 b
69 5.4 8.352702e-01 1.670540e-01 0 b
70 5.6 9.231163e-01 1.846233e-01 0 b
71 5.8 9.801987e-01 1.960397e-01 0 b
72 6.0 1.000000e+00 2.000000e-01 0 b
73 6.2 9.801987e-01 1.960397e-01 0 b
74 6.4 9.231163e-01 1.846233e-01 0 b
75 6.6 8.352702e-01 1.670540e-01 0 b
76 6.8 7.261490e-01 1.452298e-01 0 b
77 7.0 6.065307e-01 1.213061e-01 0 b
78 7.2 4.867523e-01 9.735045e-02 0 b
79 7.4 3.753111e-01 7.506222e-02 0 b
80 7.6 2.780373e-01 5.560746e-02 0 b
81 7.8 1.978987e-01 3.957974e-02 0 b
82 8.0 1.353353e-01 2.706706e-02 0 b
83 8.2 8.892162e-02 1.778432e-02 0 b
84 8.4 5.613476e-02 1.122695e-02 0 b
85 8.6 3.404745e-02 6.809491e-03 0 b
86 8.8 1.984109e-02 3.968219e-03 0 b
87 9.0 1.110900e-02 2.221799e-03 0 b
88 9.2 5.976023e-03 1.195205e-03 0 b
89 9.4 3.088715e-03 6.177431e-04 0 b
90 9.6 1.533811e-03 3.067621e-04 0 b
91 9.8 7.318024e-04 1.463605e-04 0 b
92 10.0 3.354626e-04 6.709253e-05 0 b
93 1.0 6.065307e-01 1.819592e-01 1 a
94 1.2 7.261490e-01 2.178447e-01 1 a
95 1.4 8.352702e-01 2.505811e-01 1 a
96 1.6 9.231163e-01 2.769349e-01 1 a
97 1.8 9.801987e-01 2.940596e-01 1 a
98 2.0 1.000000e+00 3.000000e-01 1 a
99 2.2 9.801987e-01 2.940596e-01 1 a
100 2.4 9.231163e-01 2.769349e-01 1 a
101 2.6 8.352702e-01 2.505811e-01 1 a
102 2.8 7.261490e-01 2.178447e-01 1 a
103 3.0 6.065307e-01 1.819592e-01 1 a
104 3.2 4.867523e-01 1.460257e-01 1 a
105 3.4 3.753111e-01 1.125933e-01 1 a
106 3.6 2.780373e-01 8.341119e-02 1 a
107 3.8 1.978987e-01 5.936961e-02 1 a
108 4.0 1.353353e-01 4.060058e-02 1 a
109 4.2 8.892162e-02 2.667649e-02 1 a
110 4.4 5.613476e-02 1.684043e-02 1 a
111 4.6 3.404745e-02 1.021424e-02 1 a
112 4.8 1.984109e-02 5.952328e-03 1 a
113 5.0 1.110900e-02 3.332699e-03 1 a
114 5.2 5.976023e-03 1.792807e-03 1 a
115 5.4 3.088715e-03 9.266146e-04 1 a
116 5.6 1.533811e-03 4.601432e-04 1 a
117 5.8 7.318024e-04 2.195407e-04 1 a
118 6.0 3.354626e-04 1.006388e-04 1 a
119 6.2 1.477484e-04 4.432451e-05 1 a
120 6.4 6.252150e-05 1.875645e-05 1 a
121 6.6 2.541935e-05 7.625804e-06 1 a
122 6.8 9.929504e-06 2.978851e-06 1 a
123 7.0 3.726653e-06 1.117996e-06 1 a
124 7.2 1.343812e-06 4.031437e-07 1 a
125 7.4 4.655716e-07 1.396715e-07 1 a
126 7.6 1.549753e-07 4.649259e-08 1 a
127 7.8 4.956405e-08 1.486922e-08 1 a
128 8.0 1.522998e-08 4.568994e-09 1 a
129 8.2 4.496349e-09 1.348905e-09 1 a
130 8.4 1.275408e-09 3.826223e-10 1 a
131 8.6 3.475891e-10 1.042767e-10 1 a
132 8.8 9.101471e-11 2.730441e-11 1 a
133 9.0 2.289735e-11 6.869205e-12 1 a
134 9.2 5.534610e-12 1.660383e-12 1 a
135 9.4 1.285337e-12 3.856012e-13 1 a
136 9.6 2.867975e-13 8.603925e-14 1 a
137 9.8 6.148396e-14 1.844519e-14 1 a
138 10.0 1.266417e-14 3.799250e-15 1 a
139 1.0 2.096114e-01 1.676891e-02 1 b
140 1.2 2.664683e-01 2.131746e-02 1 b
141 1.4 3.320399e-01 2.656320e-02 1 b
142 1.6 4.055545e-01 3.244436e-02 1 b
143 1.8 4.855369e-01 3.884295e-02 1 b
144 2.0 5.697828e-01 4.558263e-02 1 b
145 2.2 6.554063e-01 5.243250e-02 1 b
146 2.4 7.389685e-01 5.911748e-02 1 b
147 2.6 8.166865e-01 6.533492e-02 1 b
148 2.8 8.847059e-01 7.077647e-02 1 b
149 3.0 9.394131e-01 7.515305e-02 1 b
150 3.2 9.777512e-01 7.822010e-02 1 b
151 3.4 9.975031e-01 7.980025e-02 1 b
152 3.6 9.975031e-01 7.980025e-02 1 b
153 3.8 9.777512e-01 7.822010e-02 1 b
154 4.0 9.394131e-01 7.515305e-02 1 b
155 4.2 8.847059e-01 7.077647e-02 1 b
156 4.4 8.166865e-01 6.533492e-02 1 b
157 4.6 7.389685e-01 5.911748e-02 1 b
158 4.8 6.554063e-01 5.243250e-02 1 b
159 5.0 5.697828e-01 4.558263e-02 1 b
160 5.2 4.855369e-01 3.884295e-02 1 b
161 5.4 4.055545e-01 3.244436e-02 1 b
162 5.6 3.320399e-01 2.656320e-02 1 b
163 5.8 2.664683e-01 2.131746e-02 1 b
164 6.0 2.096114e-01 1.676891e-02 1 b
165 6.2 1.616212e-01 1.292970e-02 1 b
166 6.4 1.221507e-01 9.772054e-03 1 b
167 6.6 9.049144e-02 7.239315e-03 1 b
168 6.8 6.571027e-02 5.256822e-03 1 b
169 7.0 4.677062e-02 3.741650e-03 1 b
170 7.2 3.263076e-02 2.610460e-03 1 b
171 7.4 2.231491e-02 1.785193e-03 1 b
172 7.6 1.495813e-02 1.196651e-03 1 b
173 7.8 9.828195e-03 7.862556e-04 1 b
174 8.0 6.329715e-03 5.063772e-04 1 b
175 8.2 3.995846e-03 3.196677e-04 1 b
176 8.4 2.472563e-03 1.978050e-04 1 b
177 8.6 1.499685e-03 1.199748e-04 1 b
178 8.8 8.915937e-04 7.132750e-05 1 b
179 9.0 5.195747e-04 4.156597e-05 1 b
180 9.2 2.967858e-04 2.374286e-05 1 b
181 9.4 1.661699e-04 1.329359e-05 1 b
182 9.6 9.119596e-05 7.295677e-06 1 b
183 9.8 4.905836e-05 3.924669e-06 1 b
184 10.0 2.586810e-05 2.069448e-06 1 b
I have used facet_grid to generate this plot, but there is one thing that I am trying to figure out. So, the right panel is for z=0, and the left is for z=1. I want to move the line legend to inside the left panel (for Z=1) (top corner). I couldn't find the option for that.
And here is my code that I used in R to generate the plot:
plot1 <- ggplot(data=df, aes(x=xval, y=yval, group=cond, colour=cond) ) +
+ geom_smooth(aes(ymin = yval-se, ymax = yval+se, linetype=cond, colour=cond, fill=cond), stat="identity", size=1.1) +
+ scale_colour_hue(l=25) +
+ ylim(-0.1,1.3) + scale_linetype_manual(values = c('a' = 1,'b' = 2))
plot1 + facet_grid(~ zval, scales="free_y") + theme(strip.text.x = element_blank(),strip.background = element_rect(colour="white", fill="white"))
plot1 <- ggplot(data=df, aes(x=xval, y=yval, group=cond, colour=cond) ) +
geom_smooth(aes(ymin = yval-se, ymax = yval+se,
linetype=cond, colour=cond, fill=cond), stat="identity",
size=1.1) +
scale_colour_hue(l=25) +
ylim(-0.1,1.3) + scale_linetype_manual(values = c('a' = 1,'b' = 2))
The coordinates for legend.position are x- and y- offsets from the bottom-left of the plot, ranging from 0 - 1.
plot1 + facet_grid(~ zval, scales="free_y") +
theme(strip.text.x = element_blank(),
strip.background = element_rect(colour="white", fill="white"),
legend.position=c(.9,.75)
)
Tweak the legend.position values to suit your preference.

similar to excel vlookup

Hi
i have a 10 year, 5 minutes resolution data set of dust concentration
and i have seperetly a 15 year data set with a day resolution of the synoptic clasification
how can i combine these two datasets they are not the same length or resolution
here is a sample of the data
> head(synoptic)
date synoptic
1 01/01/1995 8
2 02/01/1995 7
3 03/01/1995 7
4 04/01/1995 20
5 05/01/1995 1
6 06/01/1995 1
>
head(beit.shemesh)
X........................ StWd SHT PRE GSR RH Temp WD WS PM10 CO O3
1 NA 64 19.8 0 -2.9 37 15.2 61 2.2 241 0.9 40.6
2 NA 37 20.1 0 1.1 38 15.2 344 2.1 241 0.9 40.3
3 NA 36 20.2 0 0.7 39 15.1 32 1.9 241 0.9 39.4
4 NA 52 20.1 0 0.9 40 14.9 20 2.1 241 0.9 38.7
5 NA 42 19.0 0 0.9 40 14.6 11 2.0 241 0.9 38.7
6 NA 75 19.9 0 0.2 40 14.5 341 1.3 241 0.9 39.1
No2 Nox No SO2 date
1 1.4 2.9 1.5 1.6 31/12/2000 24:00
2 1.7 3.1 1.4 0.9 01/01/2001 00:05
3 2.1 3.5 1.4 1.2 01/01/2001 00:10
4 2.7 4.2 1.5 1.3 01/01/2001 00:15
5 2.3 3.8 1.5 1.4 01/01/2001 00:20
6 2.8 4.3 1.5 1.3 01/01/2001 00:25
any idea's
Make an extra column for calculating the dates, and then merge. To do this, you have to generate a variable in each dataframe bearing the same name, hence you first need some renaming. Also make sure that the merge column you use has the same type in both dataframes :
beit.shemesh$datetime <- beit.shemesh$date
beit.shemesh$date <- as.Date(beith.shemesh$datetime,format="%d/%m/%Y")
synoptic$date <- as.Date(synoptic$date,format="%d/%m/%Y")
merge(synoptic, beit.shemesh,by="date",all.y=TRUE)
Using all.y=TRUE keeps the beit.shemesh dataset intact. If you also want empty rows for all non-matching rows in synoptic, you could use all=TRUE instead.

Resources