-1

My table looks similar to this I have a table with 15 rows and 6 columns. Let's say my column number 6 is patient's blood sugar; I want to multiply all elements that come on odd positions by 5 and leave the rest unchanged (only for that specific column). How can I do that?

if in odd positions then blood sugar * 5 ..... if in even position then leave unchanged

Thank you

r_beginner
  • 11
  • 3
  • I can many confusions going around answer to your question. It would be better if you can provide expected output for `BloodSugar` after multiplication. – MKR Apr 29 '18 at 04:59

4 Answers4

1

We can create a logical index with %% that gives TRUE for odd and FALSE for even positions. Then subset the 6th column rows based on that and assign the modified values by multiplying with 5

i1 <- as.logical(seq_len(nrow(df1)) %% 2)
df1[[6]][i1] <- df1[[6]][i1] * 5
akrun
  • 874,273
  • 37
  • 540
  • 662
0

Assuming my data is data, I'll first create a vector to store all the indexes of odd rows within the PatientProfile dataframe:

odd_ind <-seq(1,nrow(PatientProfile),2)
odd_ind
# returns: 1   3   5   7   9  11  13  15

And then subset for rows in odd_ind, and multiply 5 to the value of value in those qualifying rows:

PatientProfile[odd_ind, "BloodSugar"] <- PatientProfile[odd_ind, "BloodSugar"] *5
onlyphantom
  • 8,606
  • 4
  • 44
  • 58
  • @r_beginner try the code I provide `PatientProfile[even_ind, "BloodSugar"] <- PatientProfile[even_ind, "BloodSugar"] *5`. This should work on the dataset based on the screenshot of your table. – onlyphantom Apr 29 '18 at 04:59
  • Thank you for your help. I actually need to multiply odd positions by 5; not even! What does even_ind stand for? – r_beginner Apr 29 '18 at 05:34
  • @r_beginner fixed the code for you. Copy and run the three lines I gave you and it will work. `odd_ind` is just a vector of odds numbers going from 1 to 15 for a dataframe that has a row of 16 or less (hence `nrow(PatientProfile)`) – onlyphantom Apr 29 '18 at 05:38
  • It worked for me xoxo – r_beginner Apr 29 '18 at 05:41
  • please accept the answer @r_beginner and glad it helped you! Was my explanations about what `odd_ind` clear enough? – onlyphantom Apr 29 '18 at 05:42
0
df<-data.frame('x'=rnorm(15),
               'y'=rnorm(15))

## tag odd row position 

df$odd.row<-as.numeric(row.names(df))%%2

## change the column, say x, base on odd.row
df$x.changed<-ifelse(df$odd.row ==1, df$x*5, df$x)

Hope this would solve your problem, there may be a better way to do this, more elegantly and efficiently, someone else may know how to that. By the way, whenever you have a problem, search for an answer before simply post a question, provide a minimum reproducible example if a complete search did not help you out.

Jia Gao
  • 1,172
  • 3
  • 13
  • 26
  • Thank you for your reply. I tried your suggestion but it turned my values into (1,0) then my new column gave me weird decimal numbers – r_beginner Apr 29 '18 at 03:50
  • 1
    That's why you should provide a reproducible example. `$` is to create a new column, make sure you follow exactly the steps I posted. However, if other solutions did solve your problem, forget mine and accept that as an answer. – Jia Gao Apr 29 '18 at 04:31
0

You can use seq to index every other row:

# dummy data
df <-data.frame("x" = LETTERS[1:10], "h" = sample(c("M", "F"), replace = T, size = 10), "y" = sample(seq(1, 3, 1), replace = T, size = 10))


# index odd rows using seq
df[seq(1, length(df[,1]), 2), 3] <- df[seq(1, length(df[,1]), 2), 3] * 5

You've since commented asking whether you could use for loops or ifelse statements, yes you could (though I'm not sure why you would):

# for loop
for(i in seq(1, length(df[,1]), 2)){
  df[i,3] <- 5 * df[i,3]
}

# ifelse
library("gtools")
df[,3] <- ifelse(odd(as.numeric(rownames(df))), 5 * df[,3], df[,3])
rg255
  • 4,119
  • 3
  • 22
  • 40
  • Can I use if else or loops? Thanks – r_beginner Apr 29 '18 at 03:45
  • I tried every single suggestion on here, none of them worked for me. They absolutely sound gibberish to me "I LITERALLY CAN CRY AT THIS POINT" Can you please view the table I provided? – r_beginner Apr 29 '18 at 04:56
  • Looking at your table you are trying to multiply column 3: I'll update the code, check back in a minute – rg255 Apr 29 '18 at 04:58
  • @r_beginner done - see all the 3's in my code - that's telling it which column to focus on – rg255 Apr 29 '18 at 05:02
  • Yup, that is correct. I need to multiply 132 (location 1), 79 (location 3), 120 (location 5) , 89 (location 7), 139 (location 9)…… each by 5. – r_beginner Apr 29 '18 at 05:04
  • Nope ;( What's library("gtools")? It's giving me error message... Also odd is giving me as undefined function – r_beginner Apr 29 '18 at 05:08
  • You need to install the package: `install.packages("gtools")` – rg255 Apr 29 '18 at 05:12