r - multiplication of mixed dataframe with vector -


R initials here: What will be a simple answer for more than a day, after searching, your first question on Decide to post here:

I want to multiply (or divide) the numerical column in the dataframe with a numerical vector. The dataframe contains not only numbers, but also strings. In my search I have learned about t (mydf) * myvec , sweep () , scale () , * () to apply the replacement work, but I'm having trouble locating a smart function that allows me to allow you to specify that Detafrem is multiplied without any columns.

How to multiply / retrieve each row and a dataframe in the previous two columns of test.dat with the result that contains no result column and gt; (Yes, for numerics I could only add 1 'to myvec). But how do I deal with names? Thanks in advance!

Reasonable Example:

mydf & lt; -as.data.frame (rbind (c ( "chrX", 5624624, 5631869, "Nudt11", 2, "+", 1, 7245, 1.332, 9 651.3), C ( "Kroaks", 5,977,262, 6,210,835, " "Score", "edge", "score 2", "width", "value", "noise 4", 9, "+", 1, 233573, 1.357, 316914)) Value2 ")

myvec & lt; -c (10, 0.0001)

As already mentioned, In the example, with data.frame not working. Let us as their data data.frame before:

  # Force numerical values ​​as variable data (column) mydf & lt; -as.data.frame (cbind (c (1, 10, 3.6, 4.5, 5.4, 99), c (12, 18, 9, 8.1, 7.2, 84))) give the names of the columns: name (mydf ) & Lt; -c ( "Myname: mydf [4: 6,] & lt; -myvec * mydf [4: 6,] mydf somename alias 1 $ 1.00000 12.00000 2 10.00000 18.00000 3 3.60000 9.00000 4 40.50000 72.90000 5 54.00000 72.00000 6 16.50033 14.00028 < / Code>  

Edit: Again, your example data is not the data frame, but after touching it in the appropriate data frame, where the numeric values ​​are actually numbers and not the factors, it's just works:

  mydf [, 9: 10] & lt; -myvec * mydf [, 9: 10] mydf chr start end name score edging score 2 width value value 2 1 chrx 5624624 5631869 Nudt11 2 + 1 7245 1.332 E + 196513.0000 2 Croatia Number 5977262 6210835 showroom 9 9 1 233573 1.357e-04 31.6914   

So you can choose what you want by using the column Choir brackets, just make sure that myvec length is equal to the number of columns so that you do not get any surprising results due to recycling.

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -