Home > other >  Replace R data.frame columns values based on value length
Replace R data.frame columns values based on value length

Time:01-16

Suppose I have the following df in R:

df <- data.frame(A = c(1234, 56789, 159, 45950, 7844, 678, 17, 28794), B = c(657, 823, 465, 756, 328, 122, 490, 990))

I need to replace values in column A that have a length less than 4, not remove the values from the df entirely. Values with a length greater than 4 can remain as is. I've referenced the post Replace values based on length but have had no luck as the code doesn't perform any action on the column.

file['A'][sapply(file['A'], nchar) < 4] <- ""

Any suggestions?

CodePudding user response:

If your dataframe has characters you could use nchar to count the number of character with sapply like this:

df <- data.frame(A = as.character(c(1234, 56789, 159, 45950, 7844, 678, 17, 28794)), B = as.character(c(657, 823, 465, 756, 328, 122, 490, 990)))
df[sapply(df, \(x) nchar(x) < 4)] <- ""
df
#>       A B
#> 1  1234  
#> 2 56789  
#> 3        
#> 4 45950  
#> 5  7844  
#> 6        
#> 7        
#> 8 28794

Created on 2023-01-15 with reprex v2.0.2


If your dataframe has numeric values, you could change all values less than 1000 like this:

df <- data.frame(A = c(1234, 56789, 159, 45950, 7844, 678, 17, 28794), B = c(657, 823, 465, 756, 328, 122, 490, 990))
df[sapply(df, \(x) x < 1000)] <- ""
df
#>       A B
#> 1  1234  
#> 2 56789  
#> 3        
#> 4 45950  
#> 5  7844  
#> 6        
#> 7        
#> 8 28794

Created on 2023-01-15 with reprex v2.0.2

CodePudding user response:

Suppose to replace x for length less than 4 (assuming no float number), it might be df[df <= 999]="X"

      A B
1  1234 X
2 56789 X
3     X X
4 45950 X
5  7844 X
6     X X
7     X X
8 28794 X
  • Related