R data frame: Change value in 1 column depending on value in another

Tomikichi

I have a data frame called nurse. At the moment it contains several columns but only one (nurse$word) is relevant at the moment. I want to create a new column named nurse$w.frequency which looks at the words in the nurse$word column and if it finds the one specified, I want it to change the corresponding nurse$w.frequency value to a specified integer.

nurse <- read.csv(...)


file   word         w.frequency
1      determining
2      journey
3      journey
4      serving
5      work
6      journey
...    ...

The word frequency for determining and journey, for instance, is 1590 and 4650 respectively. So it should look like the following:

file   word         w.frequency
1      determining  1590
2      journey      4650
3      journey      4650
4      serving
5      work
6      journey      4650
...    ...

I have tried it with the an ifelse statement (below) which seems to work, however, every time I try to change the actual word and frequency it overwrites the results from before.

nurse$w.frequency <- ifelse(nurse$word == "determining", nurse$w.frequency[nurse$word["determining"]] <- 1590, "")
Daniel O

You could first initialise an empty column

nurse$w.frequency <- NA

then populated it with the data you want

nurse$w.frequency[nurse$word == "determining"] <- 1590
nurse$w.frequency[nurse$word == "journey"] <- 4650

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Assign point color depending on data.frame column value R

Multiplying column value by another value depending on value in certain column R

Sort a data frame based on another sorted column value in R

Give a data.frame an index per value of another column in r

Vectorized recoding of rows in R data frame based on value in another column

In R: Replacing value of a data frame column by the value of another data frame when between condition is matched

Change column values depending a another column value in pandas

Put specific rows at the end of data frame depending on column value

Lambda data frame reference a value in another column

report the value of a numbered column depending on the number specified in another column in R

How to get the mean value of a column depending on another column in R

Merge values of a data frame to another data frame by matching value and column name in r

Change value of a cell depending on contents of column of another google sheet

how to change the column value based on other column value in data frame?

Multiply column of data frame as per value in another data frame

How to add data from column in a data frame to a corpus based on a value from another column in R?

Finding maximum value of one column (by group) and inserting value into another data frame in R

How to filter data in a column data frame using value in another column?

Filter data frame to get only rows that have a value in column and another value in any column after first value, R

Change column value in pandas depending on some dictionary data

Change the value of a date in a data frame in R

Aggregate data frame based on column value in R

how to change the color of the graph depending on the value of the column in R

extract info from a column based on value from another column in data.frame r

Add value from one data frame into another data frame in R

Filter a dataframe by column, depending on the value of another column

Match each value of a column of a data frame with the value of a column of another data frame (if the latter exists)

Append column to data frame with text based on another column value

Sort data frame based on the value of a column and the length of a list in another column