Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Create new column with values from certain rows of other columns

$
0
0

I have a dataframe that looks like this:

df <- data.frame(Subject = c(rep("A", 6), rep("B", 6)),
Trial = c(rep(c(rep(1, 2), rep(2, 2), rep(3, 2)), 2)),
Feature_1 = c(rep(123, 2), rep(234, 2), rep(345, 2), rep(456, 2), rep(567, 2), rep(678, 2)),
Feature_2 = c(rep(321, 2), rep(543, 2), rep(654, 2), rep(765, 2), rep(876, 2), rep(987, 2)),
Feature_3 = c(rep(190, 2), rep(459, 2), rep(392, 2), rep(398, 2), rep(492, 2), rep(587, 2)),
Feature_correct = NA)

df
   Subject Trial Feature_1 Feature_2 Feature_3 Feature_correct
1        A     1       123       321       190              NA
2        A     1       123       321       190              NA
3        A     2       234       543       459              NA
4        A     2       234       543       459              NA
5        A     3       345       654       392              NA
6        A     3       345       654       392              NA
7        B     1       456       765       398              NA
8        B     1       456       765       398              NA
9        B     2       567       876       492              NA
10       B     2       567       876       492              NA
11       B     3       678       987       587              NA
12       B     3       678       987       587              NA

What I need is for the Feature_correct column to contain the values from Feature_n depending on Trial for each Subject. So, for Subject A & Trial 1, Feature_correct should have the values corresponding to Subject A & Trial 1 under Feature_1. For Subject A & Trial 2, Feature_correct should have the values corresponding to Subject A & Trial 2 under Feature_2, and so on.

This is my goal:

df$Feature_goal <- c(rep(123, 2), rep(543, 2), rep(392, 2), rep(456, 2), rep(876, 2), rep(587, 2))

head(df)
  Subject Trial Feature_1 Feature_2 Feature_3 Feature_correct Feature_goal
1       A     1       123       321       190              NA          123
2       A     1       123       321       190              NA          123
3       A     2       234       543       459              NA          543
4       A     2       234       543       459              NA          543
5       A     3       345       654       392              NA          392
6       A     3       345       654       392              NA          392

I know how to do this manually (specifying the Subject name and Trial number in the syntax), but I'd like to create a loop (or whatever else works) so that I don't have to type the name of each Subject (in my real dataset I have many participants and many "Feature" variables).

I've tried this for loop, but I get an error:

df <- for(i in 1:nrow(df$Subject)) {
 if(df$Trial %in% 1){
   df[df$Subject == i & df$Trial %in% 1,]$Feature_correct = df[df$Subject == i & df$Trial %in% 1,]$Feature_1
 }
  if(df$Trial %in% 2){
   df[df$Subject == i & df$Trial %in% 2,]$Feature_correct = df[df$Subject == i & df$Trial %in% 2,]$Feature_2
  }
  if(df$Trial %in% 3){
   df[df$Subject == i & df$Trial %in% 3,]$Feature_correct = df[df$Subject == i & df$Trial %in% 3,]$Feature_3
 }
}

> Error in 1:nrow(df$Subject) : argument of length 0

Indeed,

nrow(df$Subject)
> NULL

Would anyone know how to make this work (with the loop or in any other way)?


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>