last day (24 days later) » 

3:21 AM
1
A: Extracting and cbinding similarly named variables in a data.frame in R

akrunHere is an option with split. Wouldn't recommend to have same duplicate column names in the dataset. But, if it is really needed, after thee split, change the column names by removing the . following by one or more numbers at the end of it with sub nm1 <- Reduce(intersect, lapply(r, colnames))...

 
@rnorouzian No, it is correct. I added the lapply step in case you want to have duplicate column names as data.frame does a check for column names with make.unique and it would append .1, .2 etc at the end for every duplicate names
@rnorouzian You meant from the r should select only the common columns, right?
@rnorouzian the split is doing the split based on the similarity of column names. But, if the 'DATA' have only a single name not duplicated it would be as a separate list element
@rnorouzian I updated with only using DATA
@rnorouzian If you check the second part of the answer, it is doing exactly what you meant tbl <- table(names(DATA)); nm1 <- names(which(tbl==2)) Here we assume that column names are not duplicated from a single list element. If that is the case, it is difficult because there are no identifiers, unless there is an identifier suffix or prefix for column names from each each list element before cbinding
@rnorouzian Can you show an instance where it is not working
@rnorouzian It is 3 list element, instead of 2 you showed earlier. In that case, you need nm1 <- names(which(tbl == 2))
@rnorouzian To make it more general, use nm1 <- names(which(tbl==max(tbl)))
@rnorouzian Yes, I used that DATA only. I copy pasted it in my post
@rnorouzian How is it? I find the common names as 'AA' and 'BB'? CC is not present in one of the list elements. So CC is not common in all the three list element
 
Arun HERE is where the question came from. Suppose after after split.default() call, we will have some element (i.e., named data.frames) within the output which have names that are to be excluded. For example, if the output of split.default() gives out $Name and $out. So how to delete those named data.frames?
 
@rnorouzian okay, that means you have the name of the list as "Name" or "out". Why is the first solutioin not working for you, then? lst1[!names(lst1) %in% c("out", "Name")]
 
That's a good question! it just doesn't work, I'm preparing the data to show you.
 
@rnorouzian Can you show the names of the lst1 Do you have names(lst1) as "out", "Name" which you want to delete?
 
3:21 AM
Arun just use the updated data in this post and run everything then try to delete "out" and "Name" after split and you'll see it won't work.
 
@rnorouzian I get the expected with lst2 <- lapply(lst1, function(x) setNames(x, sub("\\.\\d+$", "", names(x)))); lst2[!names(lst2) %in% c("out", "Name")]
 
I'm confused let me check again, please.
Ahhhhh! Found the problemmm!! I have a lot more than just out and Name in my actual data! like also ar = c("out", "Name", "mdif" , "stder" , "mpre"). so if you now try: lst1[!names(lst1) %in% ar] then it fails!! So I want anything that is named as in ar to be excluded.
 
@rnorouzian Not clear. You may neeed to update with a new example
 
So simple, suppose I have a vector of several names: ar = c("out", "Name", "mdif" , "stder" , "mpre"). Now either before or after split call, I want any column in the DATA that has a name similar to those in ar to be removed completely.
 
@rnorouzian Okay in that case, why can't we use DATA <- DATA[!names(DATA) %in% ar,] and then do the split
 

last day (24 days later) »