Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change values in row based on a column value r

I am new to R with a fairly simple question, I just can't figure out the answer. For my example I will use a data frame with 3 columns, but my actual data set is 139 columns with 10000 rows.

I want to replace all of the values in a given row with NA if the value in the same row in column C contains a value < 10.

Assume that all of my columns are either number or integer values.

so I want to take the data frame:

x=data.frame(c(5,9,2),c(3,4,6),c(12,9,11))
names(x)=c("A","B","C")

and replace row 2 with NA to create

y=data.frame(c(5,"NA",2),c(3,"NA",6),c(12,"NA",11))
names(y)=c("A","B","C")

Thanks!

like image 282
Boogi Avatar asked Jan 08 '13 19:01

Boogi


People also ask

How do I change data from column to row in R?

Thus, to convert columns of an R data frame into rows we can use transpose function t. For example, if we have a data frame df with five columns and five rows then we can convert the columns of the df into rows by using as. data. frame(t(df)).

How do I change a specific value in R?

replace() function in R Language is used to replace the values in the specified string vector x with indices given in list by those given in values. It takes on three parameters first is the list name, then the index at which the element needs to be replaced, and the third parameter is the replacement values.

How do I select certain rows of data 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.


1 Answers

how about:

x[x$C <10 ,] <- NA
like image 145
user1317221_G Avatar answered Sep 27 '22 23:09

user1317221_G