[R] Referencing factors through their equivalent numeric level
Hey folks I'm sorry for bringing what must be a very simple question to R-help, but after some research I haven't been able to find a solution to my problem. Suppose I create a simple factor: [code] x-c(A,B,B,C,A) x [1] A B B C A x - as.factor(x) x [1] A B B C A Levels: A B C [/code] Now, when I see this factor in terms of its numeric level values, I get [code] as.numeric(x) [1] 1 2 2 3 1 [/code] Suppose I have [code] y - 2 [/code] I want the numeric value of y to reference the level of factor x. That is, I am looking for a function (`foo') so that I will get the following [code] foo(y) #or maybe foo(y,x) [1] B [/code] I can think of a roundabout way of doing through a user defined function, but I am sure that there is a built-in function that offers this functionality. I'd be grateful if someone could tell me what that function is. Thanks in advance -- Kaushik Krishnan (kaushik.s.krish...@gmail.com) __ R-help@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.
Re: [R] Referencing factors through their equivalent numeric level
That did it! Thanks very much Berend. On Wed, May 2, 2012 at 4:19 AM, Berend Hasselman b...@xs4all.nl wrote: On 02-05-2012, at 07:22, Kaushik Krishnan wrote: Hey folks I'm sorry for bringing what must be a very simple question to R-help, but after some research I haven't been able to find a solution to my problem. Suppose I create a simple factor: [code] x-c(A,B,B,C,A) x [1] A B B C A x - as.factor(x) x [1] A B B C A Levels: A B C [/code] Now, when I see this factor in terms of its numeric level values, I get [code] as.numeric(x) [1] 1 2 2 3 1 [/code] Suppose I have [code] y - 2 [/code] I want the numeric value of y to reference the level of factor x. That is, I am looking for a function (`foo') so that I will get the following [code] foo(y) #or maybe foo(y,x) [1] B [/code] I can think of a roundabout way of doing through a user defined function, but I am sure that there is a built-in function that offers this functionality. I'd be grateful if someone could tell me what that function is. Maybe this? levels(x)[y] Berend -- Kaushik Krishnan (kaushik.s.krish...@gmail.com) __ R-help@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.
[R] Issue with %in% - not matching identical rows in data frames
Hi folks I have two data frames. I know that the nth (let's say the 7th) row in the first data frame (sequence) is there in the second (today.sequence). When I try to check that by doing 'sequence[7,] %in% today.sequence', I get all FALSE when it should be all TRUE. I'm certain I'm making some trivial mistake. Any solutions? The code to recreate the data frames and see for yourself is: sequence - structure(list(DATE = structure(c(14549, 14549, 14553, 14550, 14557, 14550, 14551, 14550), class = Date), DATASET = c(1L, 2L, 1L, 2L, 2L, 3L, 3L, 4L), REP = c(1L, 0L, 2L, 2L, 3L, 0L, 1L, 0L), WRONGS_ABS = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), WRONGS_RATIO = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), DONE = c(1L, 1L, 0L, 1L, 0L, 1L, 0L, 0L)), .Names = c(DATE, DATASET, REP, WRONGS_ABS, WRONGS_RATIO, DONE), class = data.frame, row.names = c(NA, -8L)) today.sequence - structure(list(DATE = structure(c(14551, 14550), class = Date), DATASET = 3:4, REP = c(1L, 0L), WRONGS_ABS = c(0L, 0L), WRONGS_RATIO = c(0L, 0L), DONE = c(0L, 0L)), .Names = c(DATE, DATASET, REP, WRONGS_ABS, WRONGS_RATIO, DONE), row.names = 7:8, class = data.frame) sequence[7,] #You should see '2009-11-03 3 1 0 00' today.sequence #You can clearly see that sequence [7,] is the first row in today.sequence sequence[7,] %in% today.sequence #This should show 'TRUE TRUE TRUE TRUE TRUE TRUE'. Instead # it shows 'FALSE FALSE FALSE FALSE FALSE FALSE' Thanks -- Kaushik Krishnan (kaushik.s.krish...@gmail.com) __ R-help@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.
[R] User input when running R code in batch mode
Hi I've been stumbling over a simple issue that undoubtedly has an easy solution. I need to have some way for a user to enter some values into a data frame which R will then work on. I know that data entry should ideally be done otherwise and I should use R only for the computation, but R's data manipulation abilities makes it efficient for me to write the entire code in it. When I ran R line-by-line, I did: a - scan (what='character',n=1) 1: abcd Read 1 item a [1] abcd Everything works fine. But if I save the line a - scan (what='character', n=1); a as `test.r' and run the file in command line, I get: $ r --vanilla test.r a - scan(what='character',n=1); a 1: Read 0 items character(0) Now it's not working. I read what others had suggested on earlier threads and tried different functions, including readLines(). In line-by-line, I wrote the same lines as earlier, except using `readLines(con=stdin(),n=1)' instead of `scan(what='character', n=1)'. It worked fine. Yet, when I tried the modified lines (with readLines() instead of scan()) in batch mode, R didn't wait for user input and said `character(0)'. Is there any way to make R stop for the user to enter values when running in batch mode either by changing the way I invoke scan() or readLines() or by using any other function? Thanks in advance -- Kaushik Krishnan (kaushik.s.krish...@gmail.com) __ R-help@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.