Hi Guys, It is very simple question, but I can't find the answer! Please help me. I use R and such simple function as length() doesn't work. The result is always 1 even if my data are more then 1 observations! Do I have to load any additional library?> length(Ret_1)[1] 1> lengthfunction (x) .Primitive("length") Thank you!!! -- Best regards, Andy Fetsun [[alternative HTML version deleted]]
Well, what's Ret_1 ? It's hard to answer your question without that crucial bit of information. Try str(Ret_1) and let us know what it says. My first guess is that Ret_1 is a list, e.g.:> testdata <- list(f1=c(1,2,3,4)) > length(testdata)[1] 1> length(testdata[[1]])[1] 4> str(testdata)List of 1 $ f1: num [1:4] 1 2 3 4 But we need more information to actually be able to help you. Sarah On Thu, Jul 2, 2009 at 9:01 AM, Andriy Fetsun<fetsun at googlemail.com> wrote:> Hi Guys, > > It is very simple question, but I can't find the answer! Please help me. > > I use R and such simple function as length() doesn't work. The result is > always 1 even if my data are more then 1 observations! > > Do I have to load any additional library? > >> length(Ret_1) > [1] 1 >> length > function (x) ?.Primitive("length") > > Thank you!!! > > ---- Sarah Goslee http://www.functionaldiversity.org
Hi Andriy Fetsun, How about include a "subject title" on the subject of the email like "length funcion not work". By the say, send us the result of str(your_object). Bests milton brazil=toronto On Thu, Jul 2, 2009 at 9:01 AM, Andriy Fetsun <fetsun@googlemail.com> wrote:> Hi Guys, > > It is very simple question, but I can't find the answer! Please help me. > > I use R and such simple function as length() doesn't work. The result is > always 1 even if my data are more then 1 observations! > > Do I have to load any additional library? > > > length(Ret_1) > [1] 1 > > length > function (x) .Primitive("length") > > Thank you!!! > > -- > Best regards, > > Andy Fetsun > > [[alternative HTML version deleted]] > > ______________________________________________ > 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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
On Jul 2, 2009, at 9:01 AM, Andriy Fetsun wrote:> Hi Guys, > > It is very simple question, but I can't find the answer! Please help > me. > > I use R and such simple function as length() doesn't work. The > result is > always 1 even if my data are more then 1 observations! > > Do I have to load any additional library? > >> length(Ret_1) > [1] 1Perhaps Ret_1 is not a vector? If Ret_1 is a data.frame with one column then this would be expected behavior. It is a result of data.frames really being lists. Perhaps you should try: str(Ret_1) .... to see what sort of object you have created and then perhaps, nrow(Ret_1)>> length > function (x) .Primitive("length") >If you type a function name you get the code or something informative about where to find the code. David Winsemius, MD Heritage Laboratories West Hartford, CT
Putting your two queries together [see revised Subject ... ]: [R] (no subject)^1: Could you please help me? I am trying to load an csv-file in R, but it works wrong! My data is 0,0127 -0,0016 0,0113 0,0037 -0,0025 > > Ret X0 X0127 1 0 16 2 0 113 3 0 37 4 0 25 In this case, you need to use the "header=FALSE" option to read.csv(): Ret<-read.csv("Ret.csv", header=FALSE) since the default for read.csv() is "header=TRUE", so it assigns names to the variables (col1 and col2) according to what is in the first line. Since your first line had numeric data, it appends these to "X". Read what you get from ?read.csv [R] (no subject)^2 Hi Guys, It is very simple question, but I can't find the answer! Please help me. I use R and such simple function as length() doesn't work. The result is always 1 even if my data are more then 1 observations! Do I have to load any additional library? > length(Ret_1) [1] 1 > length function (x) .Primitive("length") Thank you!!! I surmise that "Ret_1" is the result of a command similar to your Ret<-read.csv("Ret.csv") Hence it is a dataframe. If I use your Ret<-read.csv("Ret.csv") command with your "Ret.csv" data, I get, as you did: Ret # X0 X0127 # 1 0 16 # 2 0 113 # 3 0 37 # 4 0 25 Here, Ret is a dataframe with two "columns". In fact, a dataframe is a special kind of list, one element for each column: str(Ret) # 'data.frame': 4 obs. of 2 variables: # $ X0 : int 0 0 0 0 # $ X0127: int 16 113 37 25 and each "$" is one element of the list: $X0 and $X0127. Each element is a vector of numbers in the case of your "Ret". You can see what they are separately by using the "$" operator to extract them: Ret$X0 # [1] 0 0 0 0 Ret$X0127 # [1] 16 113 37 25 So "Ret" is a list with 2 elements. Hence: length(Ret) # [1] 2 Therefore, in the case of your "Ret_1", I surmise that your "Ret_1" is a list with one element (as Sarah Goslee surmised also). In other words, if you constructed "Ret_1" by reading from a CSV file, as in Ret<-read.csv("Ret.csv") you will have a dataframe with 1 "column", namely a list with one element. Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 02-Jul-09 Time: 15:49:56 ------------------------------ XFMail ------------------------------