Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - How to apply different functions to certain rows in a column

Tags:

r

I am trying to apply different functions to different rows based on the value of a string in an adjacent column. My dataframe looks like this:

type    size
A       1
B       3
A       4
C       2
C       5
A       4
B       32
C       3

and I want to apply different functions to types A, B, and C, to give a third column column "size2." For example, let's say the following functions apply to A, B, and C:

for A: size2 = 3*size
for B: size2 = size
for C: size2 = 2*size 

I'm able to do this for each type separately using this code

df$size2 <- ifelse(df$type == "A", 3*df$size, NA)
df$size2 <- ifelse(df$type == "B", 1*df$size, NA)
df$size2 <- ifelse(df$type == "C", 2*df$size, NA)

However, I can't seem to do it for all of the types without erasing all of the other values. I tried to use this code to limit the application of the function to only those values that were NA (i.e., keep existing values and only fill in NA values), but it didn't work using this code:

df$size2 <- ifelse(is.na(df$size2), ifelse(df$type == "C", 2*df$size, NA), NA)

Does anyone have any ideas? Is it possible to use some kind of AND statement with "is.na(df$size2)" and "ifelse(df$type == "C""?

Many thanks!

like image 399
Thomas Avatar asked Feb 02 '13 23:02

Thomas


People also ask

How do I apply a function to a specific column in R?

Applying a function to each columnSetting MARGIN = 2 will apply the function you specify to each column of the array you are working with. In this case, the output is a vector containing the sum of each column of the sample data frame. You can also use the apply function to specific columns if you subset the data.

How do I specify certain rows in R?

By using bracket notation on R DataFrame (data.name) we can select rows by column value, by index, by name, by condition e.t.c. You can also use the R base function subset() to get the same results. Besides these, R also provides another function dplyr::filter() to get the rows from the DataFrame.

How do I apply a formula to an entire column in R?

Simply do the following: Select the cell with the formula and the adjacent cells you want to fill. Click Home > Fill, and choose either Down, Right, Up, or Left. Keyboard shortcut: You can also press Ctrl+D to fill the formula down in a column, or Ctrl+R to fill the formula to the right in a row.

How do I select specific rows and columns from a Dataframe in R?

To select a specific column, you can also type in the name of the dataframe, followed by a $ , and then the name of the column you are looking to select. In this example, we will be selecting the payment column of the dataframe. When running this script, R will simplify the result as a vector.


1 Answers

This might be a might more R-ish (and I called my dataframe 'dat' instead of 'df' since df is a commonly used function.

> facs <- c(3,1,2)
> dat$size2= dat$size* facs[ match( dat$type, c("A","B","C") ) ]
> dat
  type size size2
1    A    1     3
2    B    3     3
3    A    4    12
4    C    2     4
5    C    5    10
6    A    4    12
7    B   32    32
8    C    3     6

The match function is used to construct indexes to supply to the extract function [.

like image 161
IRTFM Avatar answered Oct 06 '22 02:10

IRTFM