r - Copying Attribute Data Attached To Column As Another Column -


i have data set each column having attribute storing data. meaning, columns has row wise values , attributes columns have value.

i can read data attached column attribute using attr(). however, goal capture these attribute values , replicate columns.

reading attribute

> attr(data$`column1`, "metadata")$dp.somenumber1 "6200" > attr(data$`column2`, "metadata")$dp.somenumber2 "7200" 

input data

column1 column2  -0.01   0.05  -0.01   0.05  -0.01   0.05  -0.01   0.05  -0.01   0.05  -0.01   0.05  -0.01   0.05  -0.01   0.05 

then using above code, want append values shown below.

output data

column1 somenumber1 column2 somenumber2  -0.01    6200        0.05     7200  -0.01    6200        0.05     7200  -0.01    6200        0.05     7200  -0.01    6200        0.05     7200  -0.01    6200        0.05     7200  -0.01    6200        0.05     7200  -0.01    6200        0.05     7200  -0.01    6200        0.05     7200 

how can implement recursively data more 1000 columns? each read require call attr() unique column name capture attribute data , replicate adjust column.

i getting confused on how can recursively , in optimized way.

please share suggestions, thanks.

unfortunately didn't provide reproducible example. created 1 , hope fits problem:

column1 = rep(-0.01, 8) attr(column1, "metadata")$dp.somenumber1 = "6200" column2 = rep(0.05, 8) attr(column2, "metadata")$dp.somenumber2 = "7200"  data = data.frame(column1, column2) 

using lapply can iterate on columns of dataframe. each column attributes added new column original dataframe. here code of solution:

# create function extract attributes of given column(name) create new column in original dataframe attr2col <- function(col) {   myattr = attr(data[,col], "metadata")   data[,sub("^dp\\.", "", names(myattr))] <<- myattr[[names(myattr)]] }  # iterate on colums of original dataframe lapply(names(data), attr2col) 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -