如何在R中的值为NA时跳过paste()参数

时间:2014-04-04 05:15:33

标签: r dataframe paste na

我的数据框中包含 city,state和country 列。我想创建一个串联的字符串:“城市,州,国家”。但是,我的一个城市没有州(而是NA)。我希望那个城市的字符串是“城市,乡村”。以下是创建错误字符串的代码:

# define City, State, Country
  city <- c("Austin", "Knoxville", "Salk Lake City", "Prague")
  state <- c("Texas", "Tennessee", "Utah", NA)
  country <- c("United States", "United States", "United States", "Czech Rep")
# create data frame
  dff <- data.frame(city, state, country)
# create full string
  dff["string"] <- paste(city, state, country, sep=", ")

当我显示dff$string时,我得到以下内容。请注意,最后一个字符串有NA,,不需要:

> dff["string"]
                               string
1        Austin, Texas, United States
2 Knoxville, Tennessee, United States
3 Salk Lake City, Utah, United States
4               Prague, NA, Czech Rep

如何跳过NA,,包括sep = ", "

1 个答案:

答案 0 :(得分:8)

另一种方法是事后解决它:

gsub("NA, ","",dff$string)

#[1] "Austin, Texas, United States"       
#[2] "Knoxville, Tennessee, United States"
#[3] "Salk Lake City, Utah, United States"
#[4] "Prague, Czech Rep"   

备选方案#2,一旦您data.frame被称为dff,即可使用申请

apply(dff, 1, function(x) paste(na.omit(x),collapse=", ") )