2012-10-31
R: Function to perform t-test on all possible combinations of columns in a dataframe
Sometimes I need to perform a t-test on all possible combinations of the columns of a data-frame and rather than computing them all one by one, I wrote this small function:
ttest_all_possible <- function(dataframe) {
nr_columns = dim(dataframe)[2]
results <- matrix(NA, nr_columns, nr_columns)
for (i in seq(1, nr_columns)){
for (j in seq(1, nr_columns)){
results[i, j] <- t.test(dataframe[,i], dataframe[,j])$p.value
}
}
return(results)
}
Works fine:
> test <- cbind(rnorm(10),rnorm(10), rnorm(10))
> test
[,1] [,2] [,3]
[1,] 0.64061950 -0.5643179 0.7450485
[2,] 1.90703328 -0.8000197 0.2444611
[3,] 0.96346636 -0.6278075 0.1283072
[4,] -0.25758024 -0.9657888 -0.8832595
[5,] 0.36934381 1.4149775 0.8837320
[6,] 0.60584130 0.2556500 -0.2437533
[7,] -0.24559562 -0.7774171 0.1279253
[8,] -1.53276425 0.3557177 0.1611155
[9,] -1.55474156 0.3488891 0.1025763
[10,] -0.08366046 -0.2823561 1.3618903
> ttest_all_possible(test)
[,1] [,2] [,3]
[1,] 1.0000000 0.5603004 0.6495045
[2,] 0.5603004 1.0000000 0.1817692
[3,] 0.6495045 0.1817692 1.0000000
2012-10-30
How to substutitute characters in all files of a directory using regex and perl
perl -i -pe 's/CharacterToSubstiture/SubstituteWithThis/g' myfilename
Example:
Imagine I have got these two files:
shopping_list_groceries.txt:
- 2 apples
- 3 red paprika
shopping_list_clothes.txt:
The text files now look like this:
shopping_list_groceries.txt:
- 2 apples
- 3 green paprika
shopping_list_clothes.txt:
What does the one-liner do?
Example:
Imagine I have got these two files:
shopping_list_groceries.txt:
- 2 apples
- 3 red paprika
shopping_list_clothes.txt:
- 2 pairs of socks
- 1 red t-shirt
And now I decide to not like red any more, but to prefer green:
perl -i -pe 's/red/green/g' shopping_list*txt
shopping_list_groceries.txt:
- 2 apples
- 3 green paprika
shopping_list_clothes.txt:
- 2 pairs of socks
- 1 green t-shirt
perl # use perl :)
-i # change in the file, if you skip this part, the changes will be printed to stdout
-pe # I don't know
's/red/green/g' # RegEx:
's # Substitute...
/red # ...every occurence of the string "red"...
/green # ...with the string "green"...
/g' # ... everywhere ...
shopping_list*.txt # ...in all files starting with 'shopping_list' and ending with '.txt
2012-10-02
How to print all the attributes of an object in python
from pprint import pprint
pprint (vars(my_object)) # e.g. class instance
This prints all the attributes/properties this object currently has in a neat way.
Subscribe to:
Posts (Atom)