Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally filling rows of a data frame

Tags:

dataframe

r

I have a data frame of this structure which I would like to populate:

    V1      V2  V3  V4  V5  V6  V7  V8
1   ID_CODE 0   0   0   0   0   0   0
2   THIS    0   0   0   0   0   0   0
3   ISAROW  0   0   0   0   0   0   0
4   01      0   0   0   0   0   0   0
5   02      0   0   0   0   0   0   0
6   03      0   0   0   0   0   0   0
7   ID_CODE 0   0   0   0   0   0   0
8   THESE   0   0   0   0   0   0   0
9   ARE     0   0   0   0   0   0   0
10  MORE    0   0   0   0   0   0   0
11  ROWS    0   0   0   0   0   0   0
12  01      0   0   0   0   0   0   0
13  02      0   0   0   0   0   0   0
14  03      0   0   0   0   0   0   0
15  ROW     0   0   0   0   0   0   0

And this data frame with the numbers to populate it:

  V2_1 V2_2 V2_3 V2_4 V2_5 V2_6 V2_7
1 786  786  786  786  786  786  786
2 786  786  786  786  786  786  786
3 78   78   78   78   78   78   78
4 78   78   78   78   78   78   78
5 78   78   78   78   78   78   78
6 78   78   78   78   78   78   78

These numbers are to go into columns V2:V8 and only in rows in which V1 is a number. The rows in which V1 is a string are to stay as zeroes.

like image 778
efbbrown Avatar asked Feb 26 '15 03:02

efbbrown


People also ask

How do you add conditions to a data frame?

(1) IF condition – Set of numbers Suppose that you created a DataFrame in Python that has 10 numbers (from 1 to 10). You then want to apply the following IF conditions: If the number is equal or lower than 4, then assign the value of 'True' Otherwise, if the number is greater than 4, then assign the value of 'False'

How do I add a conditional column to a data frame?

You can create a conditional column in pandas DataFrame by using np. where() , np. select() , DataFrame. map() , DataFrame.

How do you fill values in a column in a data frame?

We can use the assign() method to fill the columns with a single value. Generally, the assign() method is used to add a new column to an existing DataFrame.


1 Answers

If df1 is the original and df2 the replacement, then we can use Map to replace the subset.

## find the rows with only digits in the first column
rows <- grepl("^\\d+$", df1$V1)
## replace the subset with 'df2'
df1[rows, -1] <- Map("[<-", df1[rows, -1], df2)
df1
#         V1  V2  V3  V4  V5  V6  V7  V8
# 1  ID_CODE   0   0   0   0   0   0   0
# 2     THIS   0   0   0   0   0   0   0
# 3   ISAROW   0   0   0   0   0   0   0
# 4       01 786 786 786 786 786 786 786
# 5       02 786 786 786 786 786 786 786
# 6       03  78  78  78  78  78  78  78
# 7  ID_CODE   0   0   0   0   0   0   0
# 8    THESE   0   0   0   0   0   0   0
# 9      ARE   0   0   0   0   0   0   0
# 10    MORE   0   0   0   0   0   0   0
# 11    ROWS   0   0   0   0   0   0   0
# 12      01  78  78  78  78  78  78  78
# 13      02  78  78  78  78  78  78  78
# 14      03  78  78  78  78  78  78  78
# 15     ROW   0   0   0   0   0   0   0

Or, another way would be to use replace()

df1[rows, -1] <- Map(function(x, y) replace(x, rows, y), df1[-1], df2)
like image 187
Rich Scriven Avatar answered Oct 08 '22 10:10

Rich Scriven