similar to: When factor is better than other types, such as vector and frame?

Displaying 20 results from an estimated 20000 matches similar to: "When factor is better than other types, such as vector and frame?"

2009 Sep 16
2
How not to keep the redundant space when convert a data frame with as.matrix()?
Hi, The first column in as.matrix(x) has extra spaces (" "), which I don't want. Is there a way not to generate those spaces? Regards, Peng > x=data.frame(x=1:10,y=letters[11:20]) > as.matrix(x) x y [1,] " 1" "k" [2,] " 2" "l" [3,] " 3" "m" [4,] " 4" "n" [5,] " 5"
2009 Aug 11
3
R help from command line
Hi, I frequently need to open multiple help pages in R, which requires the start of multiple R sessions. I am wondering if there is a way to invoke the help page from the command line just like 'man'. Regards, Peng
2006 Apr 14
5
vector-factor operation
I found myself wanting to average a vector [vec] within each level of a factor [Fac], returning a vector of the same length as vec. After a while I realised that lm1 <- lm(vec ~ Fac) fitted(lm1) did what I want. But there must be another way to do this, and it would be good to be able to apply other functions than mean() in this way. Cheers, Murray -- Dr Murray Jorgensen
2009 Dec 10
3
How to figure out which the version of split is used?
There are a number of functions that are dispatched to from split(). > methods('split') [1] split.data.frame split.Date split.default split.POSIXct Is there a way to figure out which of these variants is actually dispatched to when I call split? I know that if the argument is of the type data.frame, split.data.frame will be called? Is it the case that if the argument is not
2005 Jan 20
5
Subsetting a data frame by a factor, using the level that occurs the most times
I think that title makes sense... I hope it does... I have a data frame, one of the columns of which is a factor. I want the rows of data that correspond to the level in that factor which occurs the most times. I can get a list by doing: by(data,data$pattern,subset) And go through each element of the list counting the rows, to find the maximum.... BUT I can't help thinking there's
2009 Sep 17
2
Why strsplit can be used with matrix but not data.frame?
Hi, As show in the code below, strsplit can be applied to a matrix but not a data.frame. I don't understand why R is designed in this way. Can somebody help me understand it? How to split all the strings in x$y? x=data.frame(x=1:10,y=rep("abc",10)) strsplit(x$y,'b') #Error in strsplit(x$y, "b") : non-character argument y=cbind(1:10,rep("abc",10))
2009 Sep 17
3
How to generate a matrix where each row (or column) is the same vector?
Hi, I can use the following code to generate a matrix, each column of which is 'x'. But I have to specify '5' twice in the second command. I am wondering if there is a better way to do it. > x=1:10 > matrix(rep(x,5),nc=5) > t(matrix(rep(x,5),nc=5)) Regards, Peng
2013 Dec 14
2
Change factor levels
Suppose I have a dataframe 'd' defined as L3 <- LETTERS[1:3] d0 <- data.frame(cbind(x = 1, y = 1:10), fac = sample(L3, 10, replace = TRUE)) (d <- d0[d0$fac %in% c('A', 'B'),]) x y fac 2 1 2 B 3 1 3 A 4 1 4 A 5 1 5 A 6 1 6 B 8 1 8 A Even though factor 'fac' in 'd' only has 2 levels, but it seems to bear the birthmark
2009 Aug 16
1
What is the returned type of strsplit?
Hi, I run the following program. I thought that 'b' was a matrix. But it is actually not, right? Can somebody elaborate more on the type difference between 'b' and 'c' to help me understand it better? Why 'c' is what it is now? Why 'c' is not the transpose of what it is now? Regards, Peng $ Rscript split.R >
2009 Oct 02
3
How to get duplicated items in a vector?
Hi, > x=c(rep(1,3),rep(3,2)) > x [1] 1 1 1 3 3 > duplicated(x) [1] FALSE TRUE TRUE FALSE TRUE > As shown in the above code, 'duplicated' doesn't return 'F' for the first '1' and first '3' in 'x'. I am wondering if there is a function that can return an indicator for any element whether it appears in a vector twice or more. Regards, Peng
2009 Sep 17
2
What is the best way to get a subset of a data.frame?
Hi, I want to construct a data.frame 'y' by using x$x and x$y. I think that there might be better ways to do it (because, for example, we can use a_matrix[3:5,] to extract certain rows, where 'a_matrix' is a matrix). Can somebody let know what the best way is? > a=data.frame(x=1:10,y=rep("abc",10),z=rep("xyz",10)) > a x y z 1 1 abc xyz 2 2
2009 May 22
1
regrouping factor levels
Hi all, I had some trouble in?regrouping factor levels for a variable. After some experiments, I have figured out how I can recode to modify the factor levels. I would now like some help to understand why some methods work and others don't. Here's my code : rm(list=ls()) ###some trials in recoding factor levels char<-letters[1:10] fac<-factor(char) levels(fac) print(fac) ##first
2006 Jan 23
1
Sample rows in data frame by subsets
Hi, I need to resample rows in a data frame by subsets L3 <- LETTERS[1:3] d <- data.frame(cbind(x=1, y=1:10), fac=sample(L3, 10, repl=TRUE)) x y fac 1 1 1 A 2 1 2 A 3 1 3 A 4 1 4 A 5 1 5 C 6 1 6 C 7 1 7 B 8 1 8 A 9 1 9 C 10 1 10 A I have seen this used to sample rows with replacement d[sample(nrow(d), replace=T), ] x y fac 7 1 7 B 2
2009 Dec 31
3
How x[, 'colname1'] is implemented?
I don't see where describes the implementation of '[]'. For example, if x is a matrix or a data.frame, how the lookup of 'colname1' is x[, 'colname1'] executed. Does R perform a lookup in the a hash of the colnames? Is the reference O(1) or O(n), where n is the second dim of x?
2009 Dec 31
3
How x[, 'colname1'] is implemented?
I don't see where describes the implementation of '[]'. For example, if x is a matrix or a data.frame, how the lookup of 'colname1' is x[, 'colname1'] executed. Does R perform a lookup in the a hash of the colnames? Is the reference O(1) or O(n), where n is the second dim of x?
2009 Sep 16
1
How to convert a frame to a matrix while maintaining that a number is still a number?
Hi, See the code below. 'x' is a frame. x$C1 are all numbers 1, 2 and 3. 'as.matrix(x)' convert x$C1 to strings "1", "2" and "3". I'm wondering how to maintain that the first column of 'as.matrix(x)' still numbers. Regards, Peng $ cat read.csv "C1","C2" "1","x" "2","y"
2009 Sep 17
1
How to extract data.frame columns using regex?
Hi, data.frame(x1=1:11,x2=2:12,x3=3:13,y=4:14) I want to extract all the columns that with the name 'x?'. Is there a general way to do this in R? Regards, Peng
2009 Dec 01
1
vector as data.frame element?
It seems that an vector or other non elemental data type can not be assigned to an element in the data.frame. I'm wondering what is the walk around. > li=data.frame(a=c(0,1), b=c('x','y')) > li$b[[1]]= 'x' > li$b[[2]]<- c('y','z') Error in li$b[[2]] <- c("y", "z") : more elements supplied than there are to replace
2002 Sep 09
1
No reorder.factor in R
I was using R 1.5.1 today with an old script from S-Plus and discovered that R does not have reorder.factor. It's a very simple function, but very handy for re-arranging factors in lattice graphics (among other places). Would it make sense to add this function to the base code in order to increase compatability with S-Plus and add additional functionality? reorder.factor <-
2005 Jun 10
4
data.frame to character
Hi, Excuse me for this simple question. How to convert as.data.frame to as.character? ?data.frame > L3 <- LETTERS[1:3] > L10 <- LETTERS[1:10] > d <- data.frame(cbind(x=c("XYZ"), y=L10), fac=sample(L3, 10, repl=TRUE)) > d x y fac 1 XYZ A A 2 XYZ B A 3 XYZ C A 4 XYZ D A 5 XYZ E B 6 XYZ F C 7 XYZ G A 8 XYZ H C 9 XYZ I B 10 XYZ