R sum list of data.frame with almost same structure - r

Here is my question,
I have a list of data.frames. It's produced by same piece of codes with different data.
All of the data.frames looks like
US 100 (not guarantee to exist in another data.frame because data is different)
CA 50
...
Is there any fast/neat way to sum over all the data.frames?

I am not sure whether I have understood your problem correctly, but here a possible solution:
Try to put all your dataframes in a list, e.g., your_list=list(df1,df2,...)
Then use total_df=do.call(rbind,your_list) to combine all dataframes (row-wise).
After that you can use ddply(total_df,"country",function (x) sum(x$value)) to aggregate the data. Here, I have assumed that US and CA stand for entries in a country column and 100 and 50 for entries in a value column.

Related

Merge multiple dataframes with matching and different columns and put NA's

I have 5 dataframes with different subsets of variables. For example, the subset of the 5 A-Variables appear in dataframe 1 and 5. The subset of the 7 B-Variables appear in dataframe 1 and 4 and so on. A different number of persons did one of the 5 test-versions (thats why I have 5 dataframes)
Now, I want to merge the dataframes together. The colums shall have all variables of all dataframes. When a variable appeared in two dataframes, the values should be merged and appear in one column at the end. For all persons who did not see a variable because it was in another test, a "NA" should be in there at the end..
Do you guys have an idea?
Thank you very much in advance!
You'll probably need to do some combination of inner_join(), left_join(), right_join() etc.
Check this out, should have what you need... It's difficult to know exactly what you need without seeing the data.

aggregate data.frame with formula and date variable goes wrong

I want to aggregate and count how often in my dataset is a special kind of disease at one date. (I don't use duplicate because I want all rows, not only the duplicated ones)
My original data set looks like:
id dat kinds kind
AE00302 2011-11-20 valv 1
AE00302 2011-10-31 vask 2
(of course my data.frame is much larger)
I try this:
xagg<-aggregate(kind~id+dat+kinds,subx,length)
names(xagg)<-c("id","dat","kinds","kindn")
and get:
id dat kinds kindn
AE00302 2011-10-31 valv 1
AE00302 2011-11-20 vask 1
I wonder why R is going wrong by the 'date' resp. the 'kinds'-column.
Has anybody an idea?
I still don't know why.
But I found out, aggregate goes wrong, because of columns I don't use for aggregating.
Therefor these steps solve the problem for me:
# 1st step: reduce the data.frame to only the needed columns
# 2nd Step: aggregate the reduced data.frame
# 3rd Step: merge aggregated data to reduced dataset
# 4th step: remove duplicated rows from reduced dataset (if they occur)
# 5th step: merge reduced dataset without dublicated data to original dataset
Maybe the problem occurs, if there are duplicated datasets in the aggregated data.frame.
Thanks for all your help, questions and attempts to solve my problem!
elchvonoslo

R: Function: generate and save multiple matrices based on multiple conditions

I am a new R user and an unexperienced coder and I have a data handling problem. Hopefully someone can help:
I have a data.frame with 3 columns (firm, year, class) and about 50.000 rows. I want to generate and store for every firm a (class x year) matrix with class counts as the elements in the matrix. Every matrix would be automatically named something like firm.name and stored so that I can use them afterwards for computations. Ideally, I'd be able to change the simple class counts into a function of values in columns 4 and 5 (backward and forward citations)
I am looking at 40 firms, 30 years, and about 1500 classes (so many firm-year-class counts are zero).
I realise I can get most of what I need (for counts) by simply using table(class,year,firm) as these columns have the same length. However, I don't know how to either store or access the matrices this function generates...
Any help would be greatly appreciated!
Simon
So, your question is how to deal with a table object?
Example:
#note the assigment operator
mytable <- with(ChickWeight, table(cut(weight, c(0,100,200,Inf)), Diet, Chick))
#access the data for the first chick
mytable[,,1]
#turn the table object into a data.frame
as.data.frame(mytable)

Vector of vectors in R?

I have a large set of data and I'm trying to group different rows together. I will know how to group the rows by using an ID. In the dataset, these IDs are sequential.
For example,
So what I want to do is iterate through this set of data and then place the data contained in these rows into a vector of vectors for processing later. The data contained in these rows of identical ID are going to be compared with one another to categorize the groupings.
I would like my data structure to look like something like this.
1 -> 1 -> 1
|
V
2 -> 2
So row 1 would contain only data from 1 type of ID, then the next row in the vector would be a vector of another type of ID. How would I go about doing this in R? In C++ it would just be a vector of vectors but I haven't been able to figure out how to do the same in R.
Is this even the right way to be approaching this problem? Is there a better way to do what I'm trying to do?
You would want to work with Data Frames rather than simple matrices. Have a look as the Documentation R-tutor Data.Frames.
It is doable. Best!

How to create a data.frame with 3 factors?

I hope you won't find my question too silly, i did a lot of research but it seems that i can't figure how to solve this really annoying issue.
Well, i have datas for 6 participants (P) in an experiment, with 50 trials (T) per participants and 10 condition (C). So i'd like to create a dataframe in r allowing me to put these datas.
This data.frame should have 3 factors (P, T and C) and so a number of total row of (P*T*C). The difficulty for me is to create this one, since i have the datas for the 6 participant in 6 data.frame of 100 obs(T) by 10 varibles(C).
I'd like first to create the empty dataset with these factors, and then copy the values of the 6 data.set according to the factors P, T and C.
Any help would be greatly appreciated, i'm novice in r.
Thank you.
OK; First we create one big dataframe for all participants:
result<-rbind(dfrforparticipant1, dfrforparticipant2,...dfrforparticipant6) #you'll have to fill out the proper names of the original data.frames
Next, we add a column for the participant ID:
numTrials<-50 #although 100 is also mentioned in your question
result$P<-as.factor(rep(1:6, each=numTrials))
Finally, we need to go from 'wide' format to 'long' format (I'm assuming your column names holding the results for each condition are called C1, C2 etc. ; I'm also assuming your original data.frames already held a column named T to denote the trial), like this (untested, since you did not provide example data):
orgcolnames<-paste("C", 1:10, sep="")
result2<-reshape(result, varying=list(orgcolnames), v.names="val", idvar=c("T","P"), timevar="C", times=seq_along(orgcolnames), direction="long")
What you want is now in result2.

Resources