将单元格条目粘贴到一个单元格中并删除空单元格
Pasting cell entries into one cell and deleting empty cells
我遇到以下问题:
我有一个 table 有很多空单元格。但是第一列不包含空单元格。像这样:
structure(list(variable = c("variable", "Mean (M)", "Standard",
"deviation", "(SD)", "1. Challenge"), M = c("M", "", "", "",
"", "3.06"), SD = c("SD", "", "", "", "", "1.08"), X1 = c("1",
"3.03", "1.09", "", "", ""), X2 = c("2", "2.19", "1.07", "",
"", "â0’0.06"), X3 = c("3", "1.93", "1.10", "", "", "0.52***"
), X4 = c("4", "1.86", "1.04", "", "", "â0’0.14*")), row.names = c(NA,
6L), class = "data.frame"))
How it looks like now
现在我想将 Standard (= "deviation" and "(SD)") 下的两个单元格的条目粘贴到一个单元格中,并删除这些仅包含空单元格的行。所以输出应该是这样的:
structure(list(variable = structure(c(2L, 3L, 1L), .Label = c("1. Challenge",
"Mean (M)", "Standard deviation SD", "variable"), class = "factor"),
M = structure(c(1L, 1L, 2L), .Label = c("", "3.06", "M"), class = "factor"),
SD = structure(c(1L, 1L, 2L), .Label = c("", "1.08", "SD"
), class = "factor"), `1` = structure(c(4L, 3L, 1L), .Label = c("",
"1", "1.09", "3.03"), class = "factor"), `2` = structure(c(3L,
1L, 4L), .Label = c("1.07", "2", "2.19", "â0’0.06"), class = "factor"),
`3` = structure(3:1, .Label = c("0.52***", "1.10", "1.93",
"3"), class = "factor"), `4` = structure(c(2L, 1L, 4L), .Label = c("1.04",
"1.86", "4", "â0’0.14*"), class = "factor")), row.names = 2:4, class = "data.frame")
How I would like it to look like
你能帮忙吗?
谢谢!
一个选项是根据除第一列之外的所有列中出现的空白 (""
) 创建一个分组列,然后 paste
当编号为每个列的元素时行数大于 1,得到 distinct
行
library(dplyr)
library(stringr)
df1 %>%
group_by(grp = cumsum(rowSums(.[-1] == "") != ncol(.)-1)) %>%
mutate_at(vars(-group_cols()), ~ if(n() > 1) str_c(., collapse=" ") else .) %>%
ungroup %>%
type.convert(as.is = TRUE) %>%
select(-grp) %>%
distinct
我遇到以下问题: 我有一个 table 有很多空单元格。但是第一列不包含空单元格。像这样:
structure(list(variable = c("variable", "Mean (M)", "Standard",
"deviation", "(SD)", "1. Challenge"), M = c("M", "", "", "",
"", "3.06"), SD = c("SD", "", "", "", "", "1.08"), X1 = c("1",
"3.03", "1.09", "", "", ""), X2 = c("2", "2.19", "1.07", "",
"", "â0’0.06"), X3 = c("3", "1.93", "1.10", "", "", "0.52***"
), X4 = c("4", "1.86", "1.04", "", "", "â0’0.14*")), row.names = c(NA,
6L), class = "data.frame"))
How it looks like now
现在我想将 Standard (= "deviation" and "(SD)") 下的两个单元格的条目粘贴到一个单元格中,并删除这些仅包含空单元格的行。所以输出应该是这样的:
structure(list(variable = structure(c(2L, 3L, 1L), .Label = c("1. Challenge",
"Mean (M)", "Standard deviation SD", "variable"), class = "factor"),
M = structure(c(1L, 1L, 2L), .Label = c("", "3.06", "M"), class = "factor"),
SD = structure(c(1L, 1L, 2L), .Label = c("", "1.08", "SD"
), class = "factor"), `1` = structure(c(4L, 3L, 1L), .Label = c("",
"1", "1.09", "3.03"), class = "factor"), `2` = structure(c(3L,
1L, 4L), .Label = c("1.07", "2", "2.19", "â0’0.06"), class = "factor"),
`3` = structure(3:1, .Label = c("0.52***", "1.10", "1.93",
"3"), class = "factor"), `4` = structure(c(2L, 1L, 4L), .Label = c("1.04",
"1.86", "4", "â0’0.14*"), class = "factor")), row.names = 2:4, class = "data.frame")
How I would like it to look like
你能帮忙吗? 谢谢!
一个选项是根据除第一列之外的所有列中出现的空白 (""
) 创建一个分组列,然后 paste
当编号为每个列的元素时行数大于 1,得到 distinct
行
library(dplyr)
library(stringr)
df1 %>%
group_by(grp = cumsum(rowSums(.[-1] == "") != ncol(.)-1)) %>%
mutate_at(vars(-group_cols()), ~ if(n() > 1) str_c(., collapse=" ") else .) %>%
ungroup %>%
type.convert(as.is = TRUE) %>%
select(-grp) %>%
distinct