0

I use Time Series data (imported with read.csv()) and want to match() values from an other data frame (also imported with read.csv) to those recorded in my Time Series. It looks like this:

df1 <- data.frame(hue=rawdata[,"hue"])
# This is my Time Series-raw data
                    hue
2017-07-01 00:00:00   0
2017-07-01 00:01:00   0
2017-07-01 00:02:00   0
2017-07-01 00:03:00   0
2017-07-01 00:04:00   0
2017-07-01 00:05:00   0

The Values change between 0 and 7 sometimes. Here's just a head() print

And this is the data frame I want to check it with:

df2 <- data.frame(hue=sz$hue, Q=sz$Q)
# sz is the imported csv file
  hue  Q
1   0  0
2   1 13
3   2 26
4   3 39
5   4 52
6   5 65

Here, the same: Just a head() print.

Now, my aim is to create a new column next to hue in my rawdata. in this new column I want the Q-values depending on their hue of df2. For example: From minute Zero to five on 2017-07-01 the Q-value in the new column will be 0, because hue is 0.

I tried many things with the match function like:

df1$match=sz$Q[match(df1$hue, sz$hue)]

But it's only working for the 0's and not for other values like 1,2,3 etc. R only gives me NAs at those points.

It works perfectly in this Video: Using Match function in R

Actually I'm not quite sure if this is really a "match"-problem or a more format problem because I checked these two things:

> df1["2017-07-21 23:20:00","hue"]==2 # the value at this date is actually 2!
[1] FALSE
> is.numeric(df1["2017-07-21 23:20:00","hue"])
[1] TRUE

Does anyone know what I can do to get R to consider all values? Thank you so much for taking time for this!

  • 2
    Please give a [mcve]. See [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269/4996248) for what this would mean in R. For example, if you want to ask about `df1`, post the results of `dput(head(df1))` – John Coleman Sep 19 '18 at 14:24
  • Hi John Coleman, yes, sorry for that. Here is an extract of my time series, that shows two diffrent values (0,2): `> dput(head(df1)) structure(list(hue = c(1.99999999999818, 1.99999999999818, 0)), .Names = "hue", row.names = c("2017-07-21 23:24:00", "2017-07-21 23:25:00", "2017-07-21 23:26:00"), class = "data.frame")` I wonder why these numbers are that strange and not '2' actually :/ – Lennart Sep 19 '18 at 14:46
  • I fixed it! It isn't a `match`problem. The solution is to `round` my df1 while matching it with df2 like: `df1$match=df2$Q[match(round(df1$hue), df2$hue)]`. Before, the values could not match because 1,999818 is, obviously, not the same like 2. – Lennart Sep 20 '18 at 10:06

0 Answers0