## how to change the elements in odd positions?

odd position meaning

to print the odd index elements in an array

even numbers at even index and odd numbers at odd index

**This question already has answers here**:

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

**Positive elements at even and negative at odd positions (Relative ,** We set the positive pointer at start of the array and the negative pointer at 1st position of the array. We move positive pointer two steps forward till it finds a negative In the following, the MathematicaIcon is used for padding in the reshaping of the array. After the array is reshaped, the icons are removed. Any element can be used in lieu of the MathematicaIcon, provide that one is certain that the padding element is not in the original list.

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

**Even numbers at even index and odd numbers at ,** Start from the left and keep two index one for even position and other for odd positions. Traverse these index from left. At even position there should be even number and at odd positions, there should be odd number. Whenever there is mismatch , we swap the values at odd and even index. Given an array. The task is to arrange the array such that odd elements occupy the odd positions and even elements occupy the even positions. The order of elements must remain same. Consider zero-based indexing. After printing according to conditions, if remaining, print the remaining elements as it is. Input:

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.

**How to change the elements of specific position?,** Now, I am trying to multiply all my BloodSugar that comes in odd positions by 5 and leaving the rest unchanged. Here I am getting stuck! I don't Sort even-placed elements in increasing and odd-placed in decreasing order; Product of all Subarrays of an Array; Sliding Window Maximum : Set 2; Count of subsets with sum equal to X; Maximum number of unique values in the array after performing given operations; Check if sum of Fibonacci elements in an Array is a Fibonacci number or not

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])

**Pretty way to group elements at odd and even positions ,** How to make a function that splits list elements by odd and even positions? Shortest implementation wins. I myself came up with: splitOdds[x_] := Extract[x, And this is all. The result will contain the elements placed on the following positions (0-based, so first element is at position 0, second at 1 etc.): 1, 3, 5 so the result (actual numbers) will be: 2, 4, 6 Explanation. The [1::2] at the end is just a notation for list slicing. Usually it is in the following form: some_list[start:stop:step]

**Add 3 to just the odd-index elements - MATLAB Answers,** Add 3 to just the odd-index elements. They give the answer as. b = x(1:2:end) + 3. but that makes a new matrix with just two numbers in it. output : {2 5 8 7 6 1 9 }. This is similar to an implementation of partition method of quick sort.We start from the left and keep two index one for even position and other for odd positions. Traverse these index from left. At even position there should be even number and at odd positions, there should be odd number .

**ESOP '92: 4th European Symposium on Programming, Rennes, France, ,** i) ti is a list of lists whose length is even and its elements in even positions are lists of ii) t? is equal to ti but with the elements in odd positions substituted by lists of "a" of the Let us consider for example the class of goals {:-change(t1, t2).} C program to put all even and odd elements of array in two separate array. C program to count total even and odd elements in a given array. C program to search an element in the array. C program to count frequency of each element of the array. C program to merge two array in third array.

**LATIN 2004: Theoretical Informatics: 6th Latin American Symposium, ,** An odd or even label is determined by an element's position in the input string and does not change at any stage of the algorithm. We will use O[S] and E[S] to Start from the left and keep two index one for even position and other for odd positions. Traverse these index from left. At even position there should be even number and at odd positions, there should be odd number. Whenever there is mismatch , we swap the values at odd and even index.

##### Comments

- I can many confusions going around answer to your question. It would be better if you can provide expected output for
`BloodSugar`

after multiplication. - @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. - Thank you for your help. I actually need to multiply odd positions by 5; not even! What does even_ind stand for?
- @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)`

) - It worked for me xoxo
- please accept the answer @r_beginner and glad it helped you! Was my explanations about what
`odd_ind`

clear enough? - 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
- 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. - Can I use if else or loops? Thanks
- 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?
- Looking at your table you are trying to multiply column 3: I'll update the code, check back in a minute
- @r_beginner done - see all the 3's in my code - that's telling it which column to focus on
- 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.