multiplication of mixed dataframe with vector

dataframe, r, vector

Solution

As already said, you are not dealing with the `data.frame` in your example. Let's make your data as `data.frame` first:

# bind the numerical values as variables (columns) of data.frame
mydf <-as.data.frame(cbind(
 c(1, 10, 3.6, 4.5, 5.4, 99), 
 c(12, 18, 9, 8.1, 7.2, 84)))

# give names to columns: 
names(mydf)<-c("somename","othername")

#multiply the wanted rows with myvec:

mydf[4:6,]<-myvec*mydf[4:6,]
mydf
  somename othername
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

EDIT: Again, your example data is not a data.frame, but after tweaking it to proper data frame where the numeric values really are numbers and not factors, this still works:

mydf[,9:10]<-myvec*mydf[,9:10]
mydf
   chr   start     end    name score strand score2  width     value     value2
1 chrX 5624624 5631869  Nudt11     2      +      1   7245 1.332e+01 96513.0000
2 chrX 5977262 6210835 Shroom4     9      +      1 233573 1.357e-04    31.6914

So you can choose whatever columns you want by using square brackets, just make sure the length of `myvec` is equal to the number of columns so you won't get any suprising results due to recycling.

Problem

R beginner here: After searching for what must be a simple answer for over a day, decided to post my first ever question on here: I would like to multiply (or divide) numeric columns in a dataframe with a numeric vector. The dataframe contains not just numbers but also strings. In my search I've learned about `t(t(mydf) * myvec))`, `sweep()`, `scale()`, `*apply()` and replacement operations, but I'm having trouble figuring out a clever function to allow me to specify which columns are multiplied without subsetting the dataframe. How can multiply/divide each row in the last two columns of test.dat with myvec and get back a dataframe that contains the result along with the unaltered columns> (Yes for the numerics I could just add a '1' to myvec). But how do I deal with the names? Thank you in advance!! Proper Example: mydf <-as.data.frame(rbind(c("chrX", 5624624, 5631869, "Nudt11", 2, "+", 1, 7245, 1.332, 9651.3), c("chrX", 5977262, 6210835, "Shroom4", 9, "+", 1, 233573, 1.357, 316914))) colnames(mydf)<-c("chr", "start", "end", "name", "score", "strand", "score2", "width", "value", "value2") myvec<-c(10, 0.0001)

Original source