data.frame

converting multiple lines of text into a data frame

I'm trying to find a way to convert multiple lines of text into a data frame. I'm not sure if there's a way where you can use read.delim() to read in multiple lines of text and create the following data frame with something akin to rehape()?. The data is structured as follows: A: 1 B: 2 C: 10 A: 34 B: 20 C: 6.7 A: 2 B: 78 C: 35 I'd ...

R: What are the best functions to deal with concatenating and averaging values in a data.frame?

I have a data.frame from this code: my_df = data.frame("read_time" = c("2010-02-15", "2010-02-15", "2010-02-16", "2010-02-16", "2010-02-16", "2010-02-17"), "OD" = c(0.1, 0.2, 0.1, 0.2, 0.4, 0.5) ) which produces this: > my_df r...

R: What's the easiest way to print out pairs of values from a data.frame?

I have a data.frame: df<-data.frame(a=c("x","x","y","y"),b=c(1,2,3,4)) > df a b 1 x 1 2 x 2 3 y 3 4 y 4 What's the easiest way to print out each pair of values as a list of strings like this: "x1", "x2", "y1", "y2" ...

R: How can I use apply on rows of a data.frame and get out $column_name?

I'm trying to access $a using the following example: df<-data.frame(a=c("x","x","y","y"),b=c(1,2,3,4)) > df a b 1 x 1 2 x 2 3 y 3 4 y 4 test_fun <- function (data.frame_in) { print (data.frame_in[1]) } I can now access $a if I use an index for the first column: apply(df, 1, test_fun) a "x" a "x" a "y" ...

Converting a dataframe to a vector (by rows)

I have a dataframe with numeric entries like this one test <- data.frame(x=c(26,21,20),y=c(34,29,28)) How can I get the following vector? > 26,34,21,29,20,28 I was able to get it using the following, but I guess there should be a much more elegant way X <- test[1,] for (i in 2:dim(test)[1]){ X <- cbind(X,test[i,]) } ...

writing to a dataframe from a for-loop in R

I'm trying to write from a loop to a data frame in R, for example a loop like this> for (i in 1:20) { print(c(i+i,i*i,i/1))} and to write each line of 3 values to a data frame with three columns, so that each iteration takes on a new row. I've tried using matrix, with ncol=3 and filled by rows, but only get the last item from the loop...

class "By" into dataframe in R

I'm using by() to evaluate a function by factors in my dataframe, but I need to use the results in a table form. I've seen a use of as.data.frame.table to get a "By" class object into a data frame, but I'm not sure if this only works when the number of factors employed in the by() function is the same as the length of the "by" output. U...

R selecting duplicate rows

Okay, I'm fairly new to R and I've tried to search the documentation for what I need to do but here is the problem. I have a data.frame called heeds.data in the following form (some columns omitted for simplicity) eval.num, eval.count, ... fitness, fitness.mean, green.h.0, green.v.0, offset.0, green.h.1, green.v.1,...green.h.7, green.v....

R data frame select by global variable

I'm not sure how to do this without getting an error. Here is a simplified example of my problem. Say I have this data frame DF a b c d 1 2 3 4 2 3 4 5 3 4 5 6 Then I have a variable x <- min(c(1,2,3)) Now I want do do the following y <- DF[a == x] But when I try to refer to some variable like "x" I get an erro...

R counting the occurrences of similar rows of data frame

I have data in the following format called DF (this is just a made up simplified sample): eval.num, eval.count, fitness, fitness.mean, green.h.0, green.v.0, offset.0 random 1 1 1500 1500 100 120 40 232342 2 2 1000 1250 100 120 40 11843 ...

Reading numeric Date value from CSV file to data.frame in "R"

> D <- read.csv("sample1.csv", header = FALSE, sep = ",") > D V1 V2 V3 V4 1 20100316 109825 352120 239065 2 20100317 108625 352020 239000 3 20100318 109125 352324 241065 > D[,1] [1] 20100316 20100317 20100318 In the above example how do I get the data in D[,1] to be read, and stored as date values: 2010-03-16, 201...

R: How to write out a data.frame so that I can paste it into SO for others to read?

I have a large data.frame displaying some weird properties when plotted. I'd like to ask a question about it on Stackoverflow, to do that I'd like to write the data.frame out in a form that I can paste it into SO and somebody else can easily run it and have it back into a data.frame object again. Is there an easy way to accomplish this...

Pass a data.frame column name to a function

I'm trying to write a function to accept a data.frame (x) and a column from it. The function performs some calculations on x and later returns another data.frame. I'm stuck on the best-practices method to pass the column name to the function. The two minimal examples fun1 and fun2 below produce the desired result, being able to perform ...

Summarising grouped records in a dataframe in R

Hello all, I have a data frame in R that looks like this: > TimeOffset, Source, Length > 0 1 1500 > 0.1 1 1000 > 0.2 1 50 > 0.4 2 25 > 0.6 2 3 > 1.1 1 1500 > 1.4 1 18 > 1.6 2 2500 > 1.9 2 ...

R : remove columns from dataframe where ALL values are NA

hello everybody! I'm having some trouble with my huge data frame and couldn't really resolve that question myself: The dataframe has some properties as columns and each row represents one data set. I've done some sanatizing to this dataframe (e.g. get rid of datasets which are not to be included in evaluation). (Whoever might be interest...

Summarising grouped records in a dataframe in R (...again)

Hello all, (I tried to ask this question earlier today, but later realised I over-simplified the question; the answers I received were correct, but I couldn't use them because of my over-simplification of the problem in the original question. Here's my 2nd attempt...) I have a data frame in R that looks like: "Timestamp", "Source", "...

stored values within a custom function

My program takes a data.frame and crunches the numbers. At one point, values from j-th column are multiplied by a predefined values that depends on the column name (species name, actually - it's en ecological index). So far, I've been providing these values via a second data.frame by matching column names. What would be an efficient way ...

as.data.frame of table() to summarize frequencies

In R, I'm looking for a memory-efficient way to create a summary of tabular data as follows. Take for example the data.frame foo which I've used table() to summarize, followed by as.data.frame() to obtain the frequency counts. foo <- data.frame(x= c('a', 'a', 'a', 'b', 'b', 'b'), y=c('ab', 'ac', 'ad', 'ae', 'fx', 'fy')) bar <- as.data....

Specifying column names from a list in the data.frame command.

I have a list called cols with column names in it: cols <- c('Column1','Column2','Column3') I'd like to reproduce this command, but with a call to the list: data.frame(Column1=rnorm(10)) Here's what happens when I try it: > data.frame(cols[1]=rnorm(10)) Error: unexpected '=' in "data.frame(I(cols[1])=" The same thing happens if I ...

improve my code for collapsing a list of data.frames

Dear StackOverFlowers (flowers in short), I have a list of data.frames (walk.sample) that I would like to collapse into a single (giant) data.frame. While collapsing, I would like to mark (adding another column) which rows have came from which element of the list. This is what I've got so far. This is the data.frame that needs to be co...