简体   繁体   English

删除R data.frame中的重复字段

[英]Delete repeated fields in an R data.frame

I'm very new on R and I need your help to do something that Until now i can't do: 我是R的新手,需要您的帮助才能做某些事情,直到现在我还是做不到:

I have a data frame that could have a random number of columns, I need to mantain in each column of the data frame only the unique values, but this have to be done independent of the other columns: 我有一个数据框,它的列数可以是随机的,我需要在数据框的每一列中只保留唯一值,但这必须独立于其他列来完成:

For example, if have the below data frame: 例如,如果具有以下数据框:

 Column_A   Column_B    Column_C
    A               1           A1  
    A               2           A2
    B               1           A3
    B               2           A4
    C               3           A5
    C               4           A6

The output for this, after the code must be: 在代码之后,此输出必须为:

Column_A    Column_B    Column_C
A               1           A1
B               2           A2
C               3           A3
                4           A4
                            A5
                            A6

I've tried with ds <- unique(ds) but it only will leave the unique relations between all the columns. 我已经尝试使用ds <- unique(ds)但它只会保留所有列之间的唯一关系。

I will really apreciate any help or orientation you could gave me. 我真的很感谢您能给我的任何帮助或指导。

Thanks in advance. 提前致谢。

Data 数据

`> str(df)
'data.frame':   6 obs. of  3 variables:
 $ A: chr  "A" "B" "C" "A" ...
 $ B: num  1 2 1 2 3 4
 $ C: chr  "A1" "A2" "A3" "A4" ...`

Loop

`i <- 1`
`while (i < ncol(df)){
+  df[i] <-  lapply(df, function(x) {
+           x[duplicated(x)] <- ''
+           c(x[x!=''], x[x==''])})
+ i <- i+1
+}`

If there are 'factor' columns, it is better to convert it to character or include '' as one of the levels of the factor column. 如果有“因素”列,则最好将其转换为character或包括''作为factor列的级别之一。 Here, I am changing the factor columns to character first. 在这里,我将factor列更改为character优先。

 indx <- sapply(df1, is.factor)
 df1[indx] <- lapply(df1[indx], as.character) 

Loop the columns with lapply , replace the duplicated elements with '' , arrange the elements so that the empty strings will be at the end ( c(x[x=''],x=='']) ) lapply循环列,用''替换duplicated元素,排列元素,使空字符串位于末尾( c(x[x=''],x=='']) ))

 df1[] <-  lapply(df1, function(x) {
           x[duplicated(x)] <- ''
           c(x[x!=''], x[x==''])})
 df1
 #   Column_A Column_B Column_C
 #1        A        1       A1
 #2        B        2       A2
 #3        C        3       A3
 #4                 4       A4
 #5                         A5
 #6                         A6

Or another option would be to use match 或者另一个选择是使用match

df1[] <- lapply(df1, function(x) c(x[match(unique(x),x)],
               rep('', length(x)-length(unique(x)))))

NOTE: Using '' will change the numeric column classes to 'character/factor' class. 注意:使用''会将数字列类更改为“字符/因子”类。 It may be better to replace with NA which can be easily deleted also with custom functions is.na/na.omit/complete.cases etc.. 最好用NA替换, NA也可以使用自定义函数is.na/na.omit/complete.cases等轻松删除。

data 数据

 df1 <- structure(list(Column_A = structure(c(1L, 1L, 2L, 2L, 3L, 3L), 
 .Label = c("A", 
 "B", "C"), class = "factor"), Column_B = c(1L, 2L, 1L, 2L, 3L, 
 4L), Column_C = structure(1:6, .Label = c("A1", "A2", "A3", "A4", 
 "A5", "A6"), class = "factor")), .Names = c("Column_A", "Column_B", 
 "Column_C"), row.names = c(NA, -6L), class = "data.frame")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM