Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 205372

extract values within a range across an entire dataframe in r

$
0
0

I am trying to extract specific values within a dataframe. I am attempting this because I want to gather relevant information from an output which I have saved as a dataframe so I can pull the information of interest.

I have created a dataframe that contains residual correlations. I have added a column with item names rather than use row names. I have written a function that returns the column name if the value of the residual correlation is above .15: rescorpair <- apply(res.corr, 1, function(x) paste(colnames(res.corr)[which((x > .15 & x < 1.00) |x < -.15 )], collapse = ", ")) res.corr is the dataframe with the residual correlations. This is helpful because by using the column name and row name I can identify the item pair that is above .15. I would like to create a similar function, but one that returns the value of the correlation rather than the row name.

I have included a reproducible example below, but simplified the example in the following ways. Rather than residual correlations, I just made a dataframe of correlations with a couple items.

Generate dataframe:

item1<-c(1,3,2,4,5,5)
item2<-c(2,3,5,4,5,4)
item3<-c(3,2,4,5,4,4)
items<-cbind(item1,item2,item3)
corrdata<-Hmisc::rcorr(items)
corr<-as.data.frame(corrdata$r)
corr$itemn<-c("item1","item2","item3")

I changed some things about the function that returns column names to fit the example data (I changed the range of values I wanted selected)

itemcorr <- apply(corr, 1, function(x) paste(colnames(corr)[which((x > .6 & x < 1.00) |x < -.6 )], collapse = ", "))

I would like the output with the values to look like this: item1 item2 item3 """0.60737"""

or this item1 item2 item3 """0.60737""0.60737"

This is my first question, so please let me know if I need to add any information or make any edits.


Viewing all articles
Browse latest Browse all 205372

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>