0

I have dataframe that looks like:

 Tester Type    Subject Type    Time        1     2     3
 TType1         SType1          Day 1       11    2     1         
 TType1         SType2          Day 1       3     2     13
 TType1         SType1          Day 2       2     3     15
 TType2         SType3          Day 2       1     4     3
 TType3         SType3          Day 2       2     3     4
 TType1         SType1          Day 1       7     2     2
 TType2         SType1          Day 2       2     6     7

I'm trying to find all SType 1 in Subject Type column and replace it with Values in Tester Type. So, my output would look like:

 Tester Type    Subject Type    Time        1     2     3
 TType1         TType1          Day 1       11    2     1         
 TType1         SType2          Day 1       3     2     13
 TType1         TType1          Day 2       2     3     15
 TType2         SType3          Day 2       1     4     3
 TType3         SType3          Day 2       2     3     4
 TType1         TType1          Day 1       7     2     2
 TType2         TType2          Day 2       2     6     7
3

2 Answers 2

2

We can use ifelse

ifelse(df$SubjectType == "SType1", df$TesterType, df$SubjectType)
# [1] "TType1" "SType2" "TType1" "SType3" "SType3" "TType1" "TType2"

Assuming df as your data frame.

Sign up to request clarification or add additional context in comments.

Comments

2

We can use data.table. We convert the 'data.frame' to 'data.table' (setDT(df1)), using the logical condition as 'i', we assign (:=) the 'SubjectType' that corresponds to the rows in 'i' as 'TesterType'.

library(data.table)
setDT(df1)[SubjectType=='SType1', SubjectType := TesterType]
df1
#   TesterType SubjectType  Time X1 X2 X3
#1:     TType1      TType1 Day 1 11  2  1
#2:     TType1      SType2 Day 1  3  2 13
#3:     TType1      TType1 Day 2  2  3 15
#4:     TType2      SType3 Day 2  1  4  3
#5:     TType3      SType3 Day 2  2  3  4
#6:     TType1      TType1 Day 1  7  2  2
#7:     TType2      TType2 Day 2  2  6  7

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.