Here is some hypothetical data. This may seem like a stupid example, but it gets the idea across I think. So I am looking for a correlation between age and height in various locations (think countries or whatever), but I am separating it by gender. First, I am checking to make sure that the ages between genders are comparable by running Wilcox.test. Here is a hypothetical output with an extra column I added, telling me if ages are not statistically different/comparable.
Location Age comparison p value (wilcox.test) Age comparable (p =.05)?
A 0.04 no
B 0.07 yes
C 0.09 yes
D 0.1 yes
E 0.02 no
F 0.03 no
H 0.78 yes
Then I run a Spearman cor.test, but the n for males and females whose data was collected at each of these locations is 2 or less, so it cannot be run on some of the locations, giving me a sample output of:
Correlation between age and height in males
Location Correlation p value (cor.test, pearson) Correlation present?
A 0.04 yes
B 0.17 no
H 0.47 no
Correlation between age and height in females
Location Correlation p value (cor.test, pearson) Correlation present?
C 0.08 no
D 0.03 yes
E 0.19 no
F 0.17 no
H 0.23 no
Now, I want to combine this data (age comparison and correlation) into one data frame that makes it easy to analyze the data. Here is what I want to output:
Location Age comparison p value (wilcox.test) Age comparable (p =.05)? cor.test p value (male) cor.test p value (female)
A 0.04 no 0.04 yes na na
B 0.07 yes 0.17 no na na
C 0.09 yes na na 0.08 no
D 0.1 yes na na 0.03 yes
E 0.02 no na na 0.19 no
F 0.03 no na na 0.17 no
H 0.78 yes 0.47 no 0.23 no
However, I am not sure how to line up the outputs, entering in the necessary na due to n being too small.
Could you guide me through this process please? This is my first question, so sorry if it isn't formatted well/is unclear. If you have any questions please don't hesitate to ask :)
Thanks!
merge()
to perform joins. Useall = T
if you want to include everything in both tables (union),all.x
for a left join, andall.y
for a right join. It will automatically match by the common column name if it exists, in this case Location. Otherwise you can specifyby.x
andby.y
– Mako212