blur, chart, computer

R: The best way to Upload Column to Knowledge Body In accordance with Alternative Columns

Posted on
banner 336x280

You’ll be able to virtue please see unsophisticated syntax so as to add a column to a knowledge body in R in response to the values in alternative columns:

#upload unutilized column 'col3' with values in response to columns 1 and a couple of
df$col3 <- with(df, ifelse(col1 > col2, value_if_true, value_if_false))

Please see examples display find out how to virtue this syntax in follow.

banner 468x60

Instance 1: Upload Persona Column In accordance with Alternative Columns

Please see code presentations find out how to upload a unutilized persona column in response to the values in alternative columns of the knowledge body:

#manufacture knowledge body
df <- knowledge.body(group=c('Mavs', 'Cavs', 'Spurs', 'Nets'),
                 scored=c(99, 90, 84, 96),
                 allowed=c(95, 80, 87, 95))

#view knowledge body
df

   group scored allowed
1  Mavs     99      95
2  Cavs     90      80
3 Spurs     84      87
4  Nets     96      95

#upload 'outcome' column in response to values in 'scored' and 'allowed' columns
df$outcome <- with(df, ifelse(scored > allowed, 'Win', 'Loss'))

#view up to date knowledge body
df

   group scored allowed outcome
1  Mavs     99      95    Win
2  Cavs     90      80    Win
3 Spurs     84      87   Loss
4  Nets     96      95    Win

And please see code presentations find out how to upload a unutilized persona column that mixes two ifelse() purposes to form 3 possible values in a unutilized column:

#manufacture knowledge body
df <- knowledge.body(group=c('Mavs', 'Cavs', 'Spurs', 'Nets'),
                 scored=c(99, 90, 84, 96),
                 allowed=c(95, 80, 87, 95))

#view knowledge body
df

   group scored allowed
1  Mavs     99      95
2  Cavs     90      80
3 Spurs     84      87
4  Nets     96      95

#upload 'trait' column in response to values in 'scored' and 'allowed' columns
df$trait <- with(df, ifelse(scored > 95, 'stunning',
                         ifelse(scored > 85, 'just right', 'wicked')))

#view up to date knowledge body
df

   group scored allowed trait
1  Mavs     99      95   stunning
2  Cavs     90      80    just right
3 Spurs     84      87     wicked
4  Nets     96      95   stunning

Instance 2: Upload Numeric Column In accordance with Alternative Columns

Please see code presentations find out how to upload a unutilized numeric column to a knowledge body in response to the values in alternative columns:

#manufacture knowledge body
df <- knowledge.body(group=c('Mavs', 'Cavs', 'Spurs', 'Nets'),
                 scored=c(99, 90, 84, 96),
                 allowed=c(95, 80, 87, 95))

#view knowledge body
df

   group scored allowed
1  Mavs     99      95
2  Cavs     90      80
3 Spurs     84      87
4  Nets     96      95

#upload 'lower_score' column in response to values in 'scored' and 'allowed' columns
df$lower_score <- with(df, ifelse(scored > allowed, allowed, scored))

#view up to date knowledge body
df

   group scored allowed lower_score
1  Mavs     99      95          95
2  Cavs     90      80          80
3 Spurs     84      87          84
4  Nets     96      95          95

Supplementary Assets

The best way to Upload Columns to Knowledge Body in R
The best way to Upload an Unoccupied Column to a Knowledge Body in R
The best way to Upload an Index Column to Knowledge Body in R

banner 336x280

Leave a Reply

Your email address will not be published. Required fields are marked *