Saturday, August 4, 2012
Friday, July 27, 2012
Wednesday, April 4, 2012
Matrix to vector Excel formula
Matrix is named range of matrix
Horizontal =OFFSET(Matrix,TRUNC((ROW()-ROW($A$12))/COLUMNS(Matrix)),MOD(ROW()-ROW($A$12),COLUMNS(Matrix)),1,1)
Vertical =OFFSET(Matrix,MOD(ROW()-ROW($A$12),ROWS(Matrix)),TRUNC((ROW()-ROW($A$12))/ROWS(Matrix)),1,1)
source: http://www.cpearson.com/excel/MatrixToVector.aspx
Horizontal =OFFSET(Matrix,TRUNC((ROW()-ROW($A$12))/COLUMNS(Matrix)),MOD(ROW()-ROW($A$12),COLUMNS(Matrix)),1,1)
Vertical =OFFSET(Matrix,MOD(ROW()-ROW($A$12),ROWS(Matrix)),TRUNC((ROW()-ROW($A$12))/ROWS(Matrix)),1,1)
source: http://www.cpearson.com/excel/MatrixToVector.aspx
Thursday, February 16, 2012
batch rename and (move) files using python
So, my friend asked me to rename the files. The file names are alphanumeric and they should be renamed with leading zeroes but in order. the default sort is 1,10,11….2, 20….etc but correct order would be 1,2,3,4,5…… So, I did some hit and trial and some binging, googling…
Finally done
here is the code
import osimport reimport shutildef main():## Commented out are the codes to create file for working purposes## for a in range(1,20):## for b in ["a", "b", "c"]:## filename="sec"+str(a)+b+".data"## c=open(filename,"a")## c.close()directory="oldnames"directory2="newnames"fnames=os.listdir(directory)#print(str(fnames))sortedfnames=sorted(fnames, key=natural_sortkey)#print(str(sortedfnames))for index, fname in enumerate(sortedfnames):index2=str(index).zfill(4) ##change zfill parameter to 3 if 001, 4 if 0001 etc#print str(index)+ fnameshutil.copy(os.path.join(directory,fname), os.path.join(directory2,(index2+fname)))tokenize = re.compile(r'(\d+)|(\D+)').findalldef natural_sortkey(string):return tuple(int(num) if num else alpha for num, alpha in tokenize(string))main()
Worked as magic!!
note: I got the alphanumeric sort tips from stackoverflow user(http://stackoverflow.com/users/107366/ants-aasma)
Wednesday, February 15, 2012
Alternate styling in word [With Macro]
Call me lazy,
I would rather write a script to do alternate bold and not bold of paragraphs than do it manually.
So, here is the script
sub i_am_not_lazy()
Dim para As Paragraph
Dim i As Integer
i = 1
For Each para In Selection.Paragraphs
If i Mod 2 = 0 Then
para.Range.Font.Bold = True
Else
para.Range.Font.Bold = False
End If
i = i + 1
Next
end sub
here is the result...
Dear aaa,
I would rather write a script to do alternate bold and not bold of paragraphs than do it manually.
So, here is the script
sub i_am_not_lazy()
Dim para As Paragraph
Dim i As Integer
i = 1
For Each para In Selection.Paragraphs
If i Mod 2 = 0 Then
para.Range.Font.Bold = True
Else
para.Range.Font.Bold = False
End If
i = i + 1
Next
end sub
here is the result...
Dear aaa,
I am not trying to be lazy but I have some 50 plus questions
and
I have to insert answers in between them in BOLD.
I am trying to find out if we have some smart idea to do.
Thank you for your post. I really was not looking for using CTRL B button because I have to press it everytime I enter answers.
Your response for putting macros seemed useful, but I still did not get how to use it.
Can you please guide me through the steps?
Thank you so much
aaa!!
Labels:
alternate styling,
macro,
visual basic,
word
Thursday, January 12, 2012
find and replace in batch in excel
Sub ReplaceText()
'value to replace in D, value to be replaced by in E
For Each vCell In Range("d1:d49").Cells 'range to replace b
Columns("b:b").Cells.Replace What:=vCell, Replacement:=vCell.Offset(0, 1).Value, LookAt:=xlPart, _ SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _ ReplaceFormat:=False Next vCell
End Sub
'value to replace in D, value to be replaced by in E
For Each vCell In Range("d1:d49").Cells 'range to replace b
Columns("b:b").Cells.Replace What:=vCell, Replacement:=vCell.Offset(0, 1).Value, LookAt:=xlPart, _ SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _ ReplaceFormat:=False Next vCell
End Sub
Tuesday, November 1, 2011
alpha lattice design
Background
Design:
cycdesign
alphagen
alpha+
R (agricolae)
SAS (plan?)
Analyse:
With SAS
proc lattice
package (agricolae)
Wednesday, September 14, 2011
Wednesday, September 7, 2011
Histogram in excel
Use Frequency function and Command Enter or Ctrl Shift Enter to populate through all bins...
Wednesday, September 29, 2010
Move multiple rows to Coulmns
Problem:
I took data from field which are like this
| 18-11 | 16-11 | 4-11 | 31-13 | 1-6 | 7-10 | 34-11 | 5-16 |
| 3 | _ | 3 | _ | 3 | 3 | 1 | 2 |
| 26-16 | 4-14 | 26-1 | 18-9 | 27-15 | 15-7 | 28-13 | 26-14 |
| 3 | 3 | 3 | 5 | 7 | 1 | 5 | 1 |
but I want convert it to
| 18-11 | 3 |
| 16-11 | _ |
| 4-11 | 3 |
| 31-13 | _ |
| 1-6 | 3 |
| 7-10 | 3 |
| 34-11 | 1 |
| 5-16 | 2 |
| 26-16 | 3 |
| 4-14 | 3 |
| 26-1 | 3 |
| 18-9 | 5 |
| 27-15 | 7 |
| 15-7 | 1 |
| 28-13 | 5 |
| 26-14 | 1 |
So, here is the macro
Sub alternateRowsToCol()Application.ScreenUpdating = FalseDim row, col As Integerrow = Sheets("Sheet3").UsedRange.Rows.Countcol = Sheets("Sheet3").UsedRange.Columns.CountFor i = 1 To row Step 2Sheets("Sheet3").ActivateSheets("Sheet3").Range("A" & CStr(i) & ":X" & CStr(i + 1)).Select' X here is because I have 24 rows but in example just 8Selection.CopySheets("Sheet4").ActivateSheets("sheet4").Range("A" & CStr(24 * ((i - 1) / 2) + 1)).Select' 24 here is because I have 24 rows but in example just 8Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:= _False, Transpose:=TrueNext iApplication.ScreenUpdating = TrueEnd Sub
Thursday, June 3, 2010
Insert n rows after each row in EXCEL
Sub InsertRows()
Application.ScreenUpdating = False
Dim numRows As Integer
Dim r As Long
r = Cells(Rows.Count, "A").End(xlUp).Row
numRows = 2
For r = r To 1 Step -1
Worksheets(4).Rows(r + 1).Resize(numRows).Insert
Next r
Application.ScreenUpdating = True
End Sub
split a word with
Have you ever needed to split a word with certain character, yes I do, Mostly with _ or -
here you go, an excel macro
Function split1(text)
split1 = Split(text, "_")(0)
End Function
Function split2(text)
split1 = Split(text, "_")(1)
End Function
Wednesday, June 2, 2010
INDEX and MATCH in EXCEL
So, I needed to replace a number and its related value. So easy, the formula is
=INDEX($I$2:$I$7,MATCH(B2,$H$2:$H$7,0))
Thursday, May 27, 2010
Replace words in File (Python)
#batch renamer for contig and genBank Submission#written by ananta acharya, PBGG, UGA#May 28, 2010#This program reads a .csv or .txt file with names to be replaced in each line separated with comma, for example#oldname, newname#oldname2, newname2#Then it replaces the oldnames in provided ace file with newnames#to start, u should give path of the acefile and replace file#It reports the number of lines that were replacedimport os.pathimport osdef OKtoRead(inputfile):"""Checks whether the input file is readable"""if not os.path.exists(inputfile):print "File does not Exist"return Falseelif not os.path.isfile(inputfile):print "Is not a file"return Falsereturn Truedef OKtoWrite(outputfile):"""Checks whether the input file is writable"""if not os.path.exists(os.path.dirname(outputfile)):print "Path Does Not Exist"return Falseelif os.path.isdir(outputfile):print "Is not a file but folder"return Falseelif os.path.exists(outputfile):print "File already exists, Can not Overwite or Merge"return Falseelse:return Truedef makeDict(txtFile):if OKtoRead(txtFile):txtFile=open(txtFile)renameDict={}for line in txtFile:aLine=line.strip()sets=aLine.split(",")if len(aLine)!=0:renameDict[sets[0]]=sets[1]txtFile.close()return renameDictdef replaceNames(renameDict, aceFile):aceFileNew=os.path.split(aceFile)[0]+"/renamed"+os.path.split(aceFile)[1]#if os.path.exists(aceFileNew):# os.remove(aceFileNew)if OKtoRead(aceFile):aceFile=open(aceFile,"r")newFile=open(aceFileNew, "a")renameCount=0lineCount=0for line in aceFile:lineCount+=1for key, val in renameDict.iteritems():if key in line:eachCount=line.count(key)renameCount+=1newline=line.replace(key, val)print "renaming in line %s, %s items found to replace" %(lineCount, eachCount)breakelse:newline=linenewFile.write(newline)aceFile.close()newFile.close()print "number of replaced line=%s" %renameCountprint "File written in %s" %aceFileNewdef main():aceFile=raw_input("File location for .ace: (give full location, or relative starting ../: ")while not OKtoRead(aceFile):aceFile=raw_input("File location for .ace: (give full location, or relative starting ../: ")renameFile=raw_input("File location for replacement , formatted as .txt or .csv , names separated with comma in each line: (give full location, or relative starting ../: ")while not OKtoRead(renameFile):renameFile=raw_input("File location for replacement , formatted as .txt or .csv , names separated with comma in each line: (give full location, or relative starting ../: ")dictt=makeDict(renameFile)replaceNames(dictt,aceFile)main()
Subscribe to:
Posts (Atom)
