I want make changes in big data.frame column names (names(mp)) with the help of changing a single column from other data.frame (mp$name)
#first data frame : column chooser, small data.frame with
# list of columns and other information
mp <- data.frame(name = c("A", "C", "D"), pos = 1:3) # number of rows
#in real dataset is> 5,000
name pos
1 A 1
2 C 2
3 D 3
Big dataframe, > 10,000 variables in read sense but just four is shown here
# second data.frame
mydf <- data.frame(Aa= 1:10, Ab = 11:20, Ba = 21:30, Bb = 31:40,
Ca = 41:50, Cb = 51:60, Da = 61:60,Db = 61:70)
Each values in mp$name corresponds to two variable – for example A corresponds to Aa, Ab similarly B correspond to Ba and Bb. Every such duplicated variables have suffix “a” and “b”
> mydf
Aa Ab Ba Bb Ca Cb Da Db
1 1 11 21 31 41 51 61 61
2 2 12 22 32 42 52 60 62
3 3 13 23 33 43 53 61 63
4 4 14 24 34 44 54 60 64
5 5 15 25 35 45 55 61 65
6 6 16 26 36 46 56 60 66
7 7 17 27 37 47 57 61 67
8 8 18 28 38 48 58 60 68
9 9 19 29 39 49 59 61 69
10 10 20 30 40 50 60 60 70
My idea is if something is removed from mp$name should remove the corresponding column in the dataframe mydf. For example in above example there is no “B”, thus the columns Ba and Bb should be removed from the mydf to make new dataframe mydf1
mydf1
Aa Ab Ca Cb Da Db
1 1 11 41 51 61 61
2 2 12 42 52 60 62
3 3 13 43 53 61 63
4 4 14 44 54 60 64
5 5 15 45 55 61 65
6 6 16 46 56 60 66
7 7 17 47 57 61 67
8 8 18 48 58 60 68
9 9 19 49 59 61 69
10 10 20 50 60 60 70
Although there is deletion of single var from mp (corresponding two variables from mydf is shown here) is removed, there are can be n number of such deletion and the final should maintain the order of column names or mp elements
Help is appreciated
Extract the name and their lengths. Knowing the lengths makes it easier to chop off the last letter (the a/b suffix); I’m not assuming that all the names are just 2 characters.
Subset based on column names without the last letter.