1

I'm currently working with data that contains a tuple in one of the columns and I want to test for certain values in that column. The type of that column is integer, so I cannot test for strings, hoever, R also doesn't find the values in any other way and I get only false. My data in this column looks similar to this: (1,1) and I want to test: row==(1,1). However, like this I get an error message, about the unexpected comma after row==(1. When I try row=="(1,1)" I get false as well, because typeof(row) = integer. Do you have any suggestion, how I could solve this? Thanks!

My data looks like this:

show(head(dataframe))
Generation Patch Environment
10 (1, 1) 0.3378703
10 (1, 2) 0.4121443
10 (1, 3) 2.1544672
10 (1, 4) -0.4552965
10 (1, 5) -0.6643103
10 (1, 6) 1.2926155

and I want to test (and get a boolean answer) where dataframe$Patch == (1,1). The desired answer would be somethineg like c(true, false, false, false, false, false).

Lotte Victor
  • 89
  • 1
  • 12
  • 1
    There are no tuples in R (you might come from python). Rather, most things are vectors (lists of elements of the same type) and you can create the vector you need by combining `1` and `1` using the function `c()`. For your comparison, try `all(row == c(1,1)`. Also, please look into how to create a minimal reproducible example (help us help you): https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example https://stackoverflow.com/help/mcve – Jannik Buhr Aug 06 '18 at 11:18
  • Thanks for your comment! I know that there are no tuples in R, but there are in my data. I'm not looking at how to create `c(1,1)` but on whether there is a solution on testing for tuples or some way to change the data so it can be checked. Thanks anyway! – Lotte Victor Aug 06 '18 at 11:55
  • @LotteVictor, would it be a problem to just convert the column to a character vector and then test? – Ben G Aug 06 '18 at 12:10
  • That might work. But how would I go about this? I don't create the data in R and it's a really huge dataframe...Thanks! – Lotte Victor Aug 06 '18 at 12:18
  • Did you try `dataframe$Patch == "(1, 1)"`? – Mikko Marttila Aug 06 '18 at 12:48
  • Yes I tried that, didn't work. `typeof(datafram$patch) = integer`. Thanks anyway! – Lotte Victor Aug 06 '18 at 12:54
  • 1
    @LotteVictor, pls post your data in a reproducible format so we can help. – Ben G Aug 06 '18 at 12:59
  • @BenG, I'm sorry I can't really do it better. I think, my initial problem stems from having data that does not fit the possible types of R (since I have tuples). My data is the output of julia code and I want to analyze it in R. If I try to reproduce the bit of my data I copied and pasted above inR, it will not be possible or the same problem will not occur, since I could only make the column containing `(n,n)`tuples by either creating a vector or a string. But the problem stems from it being neither. So I cannot really post anything else. I'm sorry. – Lotte Victor Aug 06 '18 at 13:13
  • 1
    @LotteVictor, how are you even reading it in then? – Ben G Aug 06 '18 at 13:40
  • We really need to know what the column `Patch` contains to be able to help you. It cannot be a tuple, as these do not exist in R, even though it looks like one. If it is a character vector, then notice the space after the comma, so trying to match without this space, as indicated in your question, won't work. Also, you need to read the help (`?row`) on `row()`, it is a function that returns an integer or factor matrix. Perhaps provide the output of `summary(dataframe)` and `mode(dataframe$Patch)`. Perhaps `Patch` is a factor, so it might contain integer values. – IanRiley Aug 06 '18 at 13:52
  • If `Patch` is a factor, the factor level names might be in the form "(n, n)". – IanRiley Aug 06 '18 at 13:56
  • I think, this is exactly the problem. When I run `typeof(datafram$patch)`it returns `integer`. If I run `mode(dataframe$patch`) it returns `numeric`. Is it possible that I just can't make R recognize this in any way or form and the most straight forward solution would be to create the output again (by running my julia code) but store the values of the tuple in two seperate columns? – Lotte Victor Aug 06 '18 at 14:00
  • Try `is.factor(dataframe$Patch)` – IanRiley Aug 06 '18 at 14:01
  • Load your data frame with `stringsAsFactors=FALSE` – IanRiley Aug 06 '18 at 14:02
  • @IanRiley `is.factor(dataframe$Patch)` returns `TRUE`. Does that mean, I can now get the values with the factor level names? How would I do that? Thanks! – Lotte Victor Aug 06 '18 at 14:03
  • Read the help on `levels()` – IanRiley Aug 06 '18 at 14:05
  • @BenG It's a .txt file and I read it in using `read.table(file.choose(), header=T, row.names=NULL, sep="\t")` – Lotte Victor Aug 06 '18 at 14:06
  • 2
    `read.table()` allows you to set `stringsAsFactors` to FALSE – IanRiley Aug 06 '18 at 14:08
  • @IanRiley: if I understand correctly, running levels on dataframe$Patch `levels(dataframe$Patch[1])` should give me the corresponding values. However, if I try to run this, I still get the whole thing. This is not what you meant, is it? – Lotte Victor Aug 06 '18 at 14:14
  • When I use `stringAsFactors=F` I can test for the string, so that's really solving my problem, thanks! I got caught up in the other suggestions before I could try this out! Thank you very much! – Lotte Victor Aug 06 '18 at 14:16
  • Yes, Lotte, it is a easy working with strings than factors (until you learn more about factors), so reading the data in as strings is the best option for now. Good luck. – IanRiley Aug 06 '18 at 15:01

0 Answers0