1

i am trying to generate 0 and 1 for absence and presence. my data is line segments and i have to plot 0 or 1 at and interval of 0.1 for points that lie within the segment or points outside the segment.

    V1   V2   V3  V4        V5 V6 V7
3   17 26.0 26.0  0 12-Jun-84  1  0
4   17 48.0 48.0  1 12-Jun-84  3  0
5   17 56.7 56.7  0 12-Jun-84  1  0
143 17 16.3 16.3  0 19-Jun-84  1  8
144 17 17.7 17.7  0 19-Jun-84  1  8
145 17 22.0 22.0  0 19-Jun-84  1  8

v2 and v3 are the start and endpoints and v4 is the separation between them.

i have tried

tran17 <- seq(0, 80, by=0.1)
tran17.date1 <- rep(0, length(tran17))
##
sub1 <-which(tran17 >= c$V2[i] & tran17 <= c$V3[i])
tran17.date1[sub1] <- 1

thankyou

alexwhan
  • 15,636
  • 5
  • 52
  • 66
Noella
  • 33
  • 4
  • 2
    Maybe you should explain why the separation btwn 48.0 and 48.0 is 1? Or else demonstrate what result is desired? And ... is this supposed to be a bunch of lines, one for each row or is V1 supposed to be a group ID? – IRTFM Jun 28 '13 at 06:37
  • the data is from a very old dataset and is as above. i am not allowed to edit it though there are such discrepancies. v1 is a row in an image. the above records are line segments on row 17. i want to generate coordinates,such that along that row i get a value every 0.1 unit or so and then get the value (0 or 1) from whether the point is within the infected section or not. i need the coordinates of these points to generate a variogram. v7 is a day number that should be the y axis and v2 the x axis. – Noella Jun 28 '13 at 08:36
  • i need to generate a bubble plot of those points – Noella Jun 28 '13 at 08:43
  • 2
    What do you mean by "not allowed to edit" ? Once you load a copy into `R` you can do whatever you want with the copy. The question is: what is the set of true values you wish to plot? – Carl Witthoft Jun 28 '13 at 11:45

2 Answers2

1

Ignoring your data example and focusing in your question, I think this solves the problem. Also, if V1 is a grouping factor, you can use tapply over PAmatrix.

# test data
sed.seed(1104) 
dat = data.frame(V1=17, V2=runif(200, 10, 60))
dat$V3 = dat$V2 + runif(200, 0, 20)
dat$V4 = dat$V3 - dat$V2

  V1       V2       V3         V4
1 17 37.25826 45.54194  8.2836734
2 17 17.44098 22.86841  5.4274331
3 17 49.78488 55.51627  5.7313965
4 17 51.66640 52.54813  0.8817293
5 17 21.84276 39.38477 17.5420079
6 17 53.39457 54.51613  1.1215530

# functions to solve the problem
isInside = function(limits, tran) as.numeric(tran>=limits[1] & tran<=limits[2])
PAmatrix = function(data, tran) t(apply(data, 1, isInside, tran=tran))

# calculate the PA matrix
tran17 = seq(0, 80, by=0.1)
PA17 = PAmatrix(data=dat[,c("V2","V3")], tran=tran17)

# plot the results
image(seq(nrow(dat)), tran17, PA17, col=c("blue", "red"))

theplot

Ricardo Oliveros-Ramos
  • 4,322
  • 2
  • 25
  • 42
  • hello Ricardo,the result i am hoping for is a bubble plot of the coordinated 1's plotted against V7. thank you. – Noella Jun 28 '13 at 08:47
  • the reason for the bubble plot is to map the points as a variable from which i should be able to generate a variogram. sorry for the unclarity. – Noella Jun 28 '13 at 09:03
  • @immie: your question is not well redacted and your data set doesn't match your description. It'll easy to help if you provide a reproducible example: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Ricardo Oliveros-Ramos Jun 28 '13 at 11:46
  • Hello Ricardo, i am sorry about that. i am new to this forum and i will try to conform as much as i can.my data set has 7 columns. the first is a the same value because it is a subset of a larger selection. v5 and is the date and it is replicated as a value in v7 according to a scale i made. i need a plot of v2 against v7 which is a plot of the points with value 1, a value which results when points spaced at 0.1 lie along a line segment line v2:v3. again,sorry for the inconvenience. thank you – Noella Jun 28 '13 at 12:40
  • Now I'm more confused. Yo didn't mention the bubble plot thing, and aparently everything you need is already in your data? Are you sure is v2 vs. v7? Try editing the question, and it's better if you provide reproducible data without inconsistencies (if your real data has inconsistencies you have to manage it other way). Normally it's easy to make things work with simple fake data and then go for the real one. – Ricardo Oliveros-Ramos Jun 28 '13 at 13:03
  • thank you very much for your feedback. let me try to arrange my question more sensibly and get back to you. – Noella Jun 28 '13 at 13:21
  • ,this is the code i have generated so far tran17 <- seq(0, 80, by=0.1) tran17.date1 <- rep(0, length(tran17)) dm <- which(c$V5 == "31-Jul-84") for(i in dm){ print(i) sub1 <-which(tran17 >= c$V2[i] & tran17 <= c$V3[i]) tran17.date1[sub1] <- 1 } plot(tran17, tran17.date1) ##with this code i can plot one row for one day. i have been trying to find a way to plot many rows for one day. possibly with a 3d plot of length 0-80 on x-axis,row numbers 0-32 on the y axis and the point value of 0 or 1 on the z axis. Is this possible? – Noella Jun 29 '13 at 19:07
  • @Noella: Edit your original post, and try to correct the data. V4 is wrong, and why V2=V3? That's mean V2:V3 is just a point! It's easy if you provide data we can run, can be fake one like I used in the answer but keeping the main characteristics of your real data, so you can use the script just changing the dataframe. It's very important we can run the code you provide. – Ricardo Oliveros-Ramos Jun 29 '13 at 19:17
0
tran17 <- seq(0, 80, by=0.1)

tran17.date1 <- rep(0, length(tran17))

dm <- which(c$V5 == "31-Jul-84")

for(i in dm){ print(i) sub1 <-which(tran17 >= c$V2[i] & tran17 <= c$V3[i]) tran17.date1[sub1] <- 1

}

plot(tran17, tran17.date1)

Noella
  • 33
  • 4