-1

I have a dataframe looking like this

---- MAG1 MAG2 MAG3 MAG4
Sample1 241 32 748 8
Sample2 34 9 0 353
Sample3 231 675 48 9
Sample4 3 88 276 0

I want to generate a loop where I will specify "Select cell value of MAG1 of Sample1 and replace that value with every other values (MAG2,MAG3,MAG4) of that same sample (Sample1) and make a new data frame in each case.

So, the first new dataframe will look like,

---- MAG1 MAG2 MAG3 MAG4
Sample1 34 32 748 8
Sample2 34 9 0 353
Sample3 231 675 48 9
Sample4 3 88 276 0

And the second new dataframe will look like,

---- MAG1 MAG2 MAG3 MAG4
Sample1 231 32 748 8
Sample2 34 9 0 353
Sample3 231 675 48 9
Sample4 3 88 276 0

Thanks in advance.

  • Your expected output does not match your instructions. Do you want to replace the first value in MAG1 with all other values in the same variable MAG1 (but different samples 2,3,4...)? – GuedesBF Aug 20 '21 at 17:05
  • You are right. My MIstake!!! Very sorry about that. I want to replace the value of MAG1 with other MAG values (MAG2, MAG3, MAG4) of Sample1 (32,748,8). – Tahsin Khan Aug 21 '21 at 00:51
  • Ok please edit your question then – GuedesBF Aug 21 '21 at 13:07

2 Answers2

1

Welcome to SO! Next time read this before posting, it's going to be easier to help.
However, if you need a loop:

# edit
for( i in 2:ncol(df)){
                     df[1,2]<- df[1,i]
                     print(df)
                     }

   X... MAG1 MAG2 MAG3 MAG4
1 Sample1    8   32  748    8
2 Sample2   34    9    0  353
3 Sample3  231  675   48    9
4 Sample4    3   88  276    0
     X... MAG1 MAG2 MAG3 MAG4
1 Sample1   32   32  748    8
2 Sample2   34    9    0  353
3 Sample3  231  675   48    9
4 Sample4    3   88  276    0
     X... MAG1 MAG2 MAG3 MAG4
1 Sample1  748   32  748    8
2 Sample2   34    9    0  353
3 Sample3  231  675   48    9
4 Sample4    3   88  276    0
     X... MAG1 MAG2 MAG3 MAG4
1 Sample1    8   32  748    8
2 Sample2   34    9    0  353
3 Sample3  231  675   48    9
4 Sample4    3   88  276    0

In case you need to store it in a list:

listed <- list()

# edit
for( i in 2:ncol(df)){
                     df[1,2]<- df[1,i]
                     listed[[i]] <- df
                      }

With data:

df <- read.table(text = '
    ---     MAG1    MAG2    MAG3    MAG4
Sample1     241     32  748     8
Sample2     34  9   0   353
Sample3     231     675     48  9
Sample4     3   88  276     0', header = T)
  
s__
  • 9,270
  • 3
  • 27
  • 45
  • Thanks for your advice, I'll go through the FAQ page first. I have made a mistake in my expected output. Very sorry about that. I want to replace the value of MAG1 with other MAG values (MAG2, MAG3, MAG4) of Sample1 (32,748,8). – Tahsin Khan Aug 21 '21 at 00:54
  • @TahsinKhan sorry for late answer, see edit. – s__ Aug 31 '21 at 08:53
0

Please review your instructions and expected output. That said, you expected output can be obtained with a loop function.


list_of_dataframes <- lapply(df$MAG1, function(x) {df[2,1]<-x; df})
GuedesBF
  • 8,409
  • 5
  • 19
  • 37