1

我有一个非常混乱且庞大的数据框,我需要对其进行组织。我已经很久没有使用 R 了,所以任何帮助将不胜感激。

例如,我的数据框如下所示:

x1 = c("a", "c", "b") 
x2 = c("b", "a", "") 
x3 = c("b", "c", "a") 
df = data.frame(x1, x2, x3)

并且需要重新组织以对齐行,如下所示:

x1 = c("a", "b", "c") 
x2 = c("a", "b", "") 
x3 = c("a", "b", "c") 
df = data.frame(x1, x2, x3)

有人可以帮忙吗?

4

3 回答 3

0

尝试:

x1 = c("a", "c", "b") 
x2 = c("b", "a", "") 
x3 = c("b", "c", "a") 
df = data.frame(x1, x2, x3)
data.frame(lapply(df, function(x) {
  res <- as.character(x)
  res[res == ""] <- NA
  res <- sort(res, na.last = TRUE)
  res[is.na(res)] <- ""
  res <- as.factor(res)
  res
}))
于 2018-07-18T11:35:49.293 回答
0
x1 = c("a", "c", "b") 
x2 = c("b", "a", "") 
x3 = c("b", "c", "a") 
df = data.frame(x1, x2, x3)

library(dplyr)

# function that orders non blank values and then adds blanks (if they exist)
f = function(x) c(sort(x[x!=""]), x[x==""])

# apply function to each column using character columns/variables
df %>% mutate_all(~f(as.character(.)))

#   x1 x2 x3
# 1  a  a  a
# 2  b  b  b
# 3  c     c
于 2018-07-18T11:29:57.447 回答
0

需要明确的是,x1、x2 和 x3 实际上是列。您的原始 data.frame 如下所示:

  x1 x2 x3
1  a  b  b
2  c  a  c
3  b     a

您是否将行与列混淆?x1 = c("a", "c", "b") - 将 x1 视为列标题或特征,而 "a"、"c" 和 "b" 是该列的元素。这可能会令人困惑,因为您是按行输入的。

于 2018-07-18T11:04:49.967 回答