简体   繁体   English

填充 R data.frame 中每一行中的缺失元素

[英]fill missing elements in each row in a R data.frame

i have a csv file imported as data.frame, the problem is that each row must have 4 elements (4 columns), and some of them could have different number of elements, i mean something like:我有一个 csv 文件作为 data.frame 导入,问题是每行必须有 4 个元素(4 列),其中一些可能有不同数量的元素,我的意思是:

ID  col1 col2 col3 col4
id1  dA  dB    dC   dD
id2  aA  aB    aC   aD
id3  mA  mB    mC
id4  xA  xB    xC   XD

I'm using tidyr, and when I import the data it fill each missing element with NA, in this case the id3 at the col4.我正在使用 tidyr,当我导入数据时,它会用 NA 填充每个缺失的元素,在本例中是 col4 处的 id3。

id3  mA  mB    mC NA

I want to fix all the row that have less than 4 elements in each row (like id3), just to add in the missing element a unclassified (UNC) something like:我想修复每行中少于 4 个元素的所有行(如 id3),只是在缺少的元素中添加一个未分类的(UNC),例如:

ID  col1 col2 col3 col4
id1  dA  dB    dC   dD
id2  aA  aB    aC   aD
id3  mA  mB    mC  UNC
id4  xA  xB    xC   XD

Well this is my code:好吧,这是我的代码:

df <- read.csv("file.csv", comment.char = "#", header = TRUE, sep = "\t")

#add the id as row name:
rownames(df) <- paste("id", 1:nrow(df), sep = "")

# eliminate some elements of the data frame 
df[, 2:ncol(df)] <- NULL

# add a name of each column and split elements based in ";" character 
#at this point the "df" has a single column named "old_name":

df <- df %>% tidyr::separate(old_name, c("col1", "col2", "col3", "col4"), sep = ";", extra="drop")

any suggestion !!!任何建议!

thanks so much非常感谢

We can use我们可以用

library(dplyr)
df1 %>%
    mutate_if(is.character, ~ replace(., is.na(.), "UNC"))

Or in base R或在base R

i1 <- sapply(df1, is.character)
df1[i1][is.na(df1[i1])] <- "UNC"

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

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