[R] R combining vectors into a data frame but without a continuous common variable

Rui Barradas ruipbarradas at sapo.pt
Thu Oct 4 19:42:38 CEST 2012


Try the following.

url1 <- "http://r.789695.n4.nabble.com/file/n4644986/Landeck_vec.txt"
url2 <- "http://r.789695.n4.nabble.com/file/n4644986/Kaurnetal_vec.txt"

dat1 <- read.table(url1, header = TRUE)
dat2 <- read.table(url2, header = TRUE)

str(dat2)  # Precip is a factor, so convert to numeric
dat2$Precip <- as.numeric(levels(dat2$Precip)[dat2$Precip])

dat1$Landeck <- as.Date(dat1$Landeck, format = "%d.%m.%Y")
dat2$Date <- as.Date(dat2$Date, format = "%d.%m.%Y")

dat3 <- merge(dat1, dat2, by.x = "Landeck", by.y = "Date")
head(dat3, 20)  # See first 20 rows

Hope this helps,

Rui Barradas
Em 04-10-2012 12:18, lucy88 escreveu:
> Hello,
> I have two different files which I'd like to combine to make one data frame
> but I've no idea how to do it! The first file has two columns; one is the
> date, the following is a binary code for debris flow events. Then my other
> file has also two columns; the date and then precipitation data.
> The thing is, is that the two date columns don't all contain the same dates.
> The binary one is every day from April - October from 1900 - 2005, yet the
> precipitation file has dates from from say, 1911 to 2004, with some missing
> data on certain months and during certain years.
> So my question is how to make a data frame which would have the date, the
> binary 0 or 1, and then the corresponding precip value from that particular
> date. I only want the precip information for the days where I have
> information in the binary file; the others can be disregarded.
> I have tried using codes which I found in answer to other questions asked
> but none of them work with my issue. If I'm honest I don't really know if
> this is what I need. I'm hoping to end up doing a logistic regression. I've
> uploaded the two files in case I've not been very clear...
> I'd be really grateful if anyone could help me and suggest a way to do it!
> I'm also really not very technical and am not at all comfortable with R so
> if you could be really basic in your advice I'd appreciate it!
> Many thanks in advance,
> Lucy
> Landeck_vec.txt
> <http://r.789695.n4.nabble.com/file/n4644986/Landeck_vec.txt>
> Kaurnetal_vec.txt
> <http://r.789695.n4.nabble.com/file/n4644986/Kaurnetal_vec.txt>
> --
> View this message in context: http://r.789695.n4.nabble.com/R-combining-vectors-into-a-data-frame-but-without-a-continuous-common-variable-tp4644986.html
> Sent from the R help mailing list archive at Nabble.com.
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list