Replacing select columns in a data frame with new columns of equal length
By : SweetPopcorn
Date : March 29 2020, 07:55 AM
To fix the issue you can do I have a data frame which I will call "abs.data" that contains 265 columns (variables). I have another data frame which I will call "corr.abs" that contains updated data on a subset of the columns in "abs.data". Both data frames have an equal number of rows, n=551. I need to replace the columns in "abs.data" with the correct observations in "corr.abs" where the column names match. I have tried the following , you can find the common column names using code :
comm_col < intersect(colnames(abs.samps), colnames(corr.abs))
x<subset(abs.samps, select = X2)
y<cbind(corr.abs$X2,x)

R: how to select data from a data frame based on specific rules and add the data as new columns to an existing data fram
By : Rajat Mhetre
Date : March 29 2020, 07:55 AM
this one helps. I have two data frames df1 and df2. , if i am understanding your question correctly this should work: code :
### we use the matches to pick our values from df1
### we use our conditions to pick our values from df2
matches < match(df2$y1,df1$x1)
matches < matches[!is.na(matches)]
condition1 < df2$y1 %in% df1$x1
condition2 < df2$y2[condition1] >= df1$x2[matches]
condition3 < df2$y2[condition1] <= df1$x3[matches]
### i create these tmp variables so you can see step by step
### what each line of code is doing
### here i am finding the values that meet all the conditions
### then i am pulling the associated y2 values
tmp < data.frame(x1=df1$x1[matches],y2=df2$y2[condition1])
tmp < tmp[condition2&condition3,]
tmp < droplevels(tmp)
### now that we have the values we want
### we are organizing the data in the desired output you
### specified.
x < split(tmp[1], tmp[[1]])
tmp2 < data.frame()
for(i in 1:length(x)){
df < data.frame(t(unlist(x[[i]], use.names=FALSE)))
colnames(df) < seq(1,nrow(x[[i]]))
tmp2 < rbind.fill(tmp2,df)
}
colnames(tmp2) < paste(rep("z",ncol(tmp2)),1:ncol(tmp2),sep="")
res < data.frame(df1[df1$x1 %in% names(x),],tmp2)
res < rbind.fill(res,df1[!df1$x1 %in% names(x),])
>res
x1 x2 x3 z1 z2
1 A35 8 15 9 NA
2 A41 24 63 24 32
3 A49 33 54 NA NA

Select a consecutive range of data.frame columns using names of beginning and end columns
By : Mohamed Medhat
Date : March 29 2020, 07:55 AM
I wish this helpful for you I am trying to subset the columns of a data.frame using the interval of column names. , Use c() function, then it works when using column names code :
> A < data.frame(ID1=c(1,1),ID2=c(5,5),ID3=c(01901,01902))
> A
# ID1 ID2 ID3
# 1 1 5 1901
# 2 1 5 1902
> b < A[,c(2:3)]
> b
# ID2 ID3
# 1 5 1901
# 2 5 1902
> b1 < A[,c("ID2","ID3")]
> b1
# ID2 ID3
# 1 5 1901
# 2 5 1902
> b2 < A[,2:3]
> b2
# ID2 ID3
# 1 5 1901
# 2 5 1902

Using data frame values to select columns of a different data frame
By : suhas harish
Date : March 29 2020, 07:55 AM
Hope that helps Using R it's important to keep your terminology precise. double_expression_combinations is not a dataframe but rather a matrix. It's easy to loop over columns in a matrix with apply. I'm a bit unclear about the exact test, but this might succeed: code :
apply( double_expression_combinations, 2, # the 2 selects each column in turn
function(cols){ sum( initial[ , cols[1] ] + initial[ , cols[2] ] == 2) } )

Select rows of the multilevel index data frame with index values equal to columns in another data frame in pandas
By : user1806699
Date : March 29 2020, 07:55 AM
Any of those help Use Index.isin with MultiIndex: code :
df = df1[df1.index.isin(df2.set_index(['i1','i2']).index)]
df = df1[df1.index.isin(pd.MultiIndex.from_arrays([df2['i1'], df2['i2']]))]
df = df1.reset_index().merge(df2).set_index(['i1', 'i2'])
df = df2.join(df1, on=['i1', 'i2'], how='inner').set_index(['i1', 'i2'])
print (df)
c
i1 i2
a1 b1 1
a3 b3 3

