简体   繁体   English

在数据框中添加字符串列(行)

[英]Adding columns of strings (row-wise) in a dataframe

I have a data frame with columns that are all strings. 我有一个数据框架,其中的列都是字符串。 There are occasional NA 's as well. 偶尔也有NA I want to add all the columns into one vector row-by-row so that I can get only the unique strings in the entire data frame. 我想将所有列逐行添加到一个向量中,这样我就只能在整个数据帧中获得唯一的字符串。 Given a sample data like this: 给定这样的样本数据:

        L1     L2    L3    L4
1      bed bicyle  <NA>  bird
2     <NA>   road   cat dance
3      cat   rock table   bed
4 mountain    bed clock  <NA>

Then I want something like this: 然后我想要这样的东西:

                                                     Total
1 bed bicyle bird road cat dance rock table mountain clock

I can obviously do this with a loop, but I am trying to figure out an apply or plyr type solution (which is more R -like). 我显然可以通过循环来做到这一点,但是我试图找出一个apply或plyr类型的解决方案(更像R )。 So far I can get a list of arrays but I then get stuck trying to add those arrays together. 到目前为止,我可以得到一个数组列表,但随后我试图将这些数组加在一起而陷入困境。 Is my approach even correct? 我的方法正确吗? Any help is appreciated. 任何帮助表示赞赏。

df <- data.frame(L1 = c("bed", NA, "cat", "mountain"), 
                 L2 = c("bicyle", "road", "rock", "bed"), 
                 L3 = c(NA, "cat", "table", "clock"), 
                 L4 = c("bird", "dance", "bed", NA))
df <- lapply(df, unique)

You can simply do this: 您可以简单地做到这一点:

unique(as.vector(t(df)))

Or, whitout NAs : 或者,whitout NAs

unique(na.omit(as.vector(t(df))))

Here's one way to accomplish your task: 这是完成任务的一种方法:

require(dplyr)
df <- data.frame(L1 = c("bed", NA, "cat", "mountain"), 
                 L2 = c("bicyle", "road", "rock", "bed"), 
                 L3 = c(NA, "cat", "table", "clock"), 
                 L4 = c("bird", "dance", "bed", NA), 
                 stringsAsFactors = FALSE)

df <- df %>% unlist() %>% na.omit() %>% unique()

Output: 输出:

 [1] "bed"      "cat"      "mountain" "bicyle"   "road"     "rock"     "table"    "clock"    "bird"     "dance"   

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

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