如何将一组列复制到新行中?
How do I copy a set of columns into new rows?
抱歉,如果之前有人回答过这个问题,我什至不确定如何搜索它。我对 R、VBA 或 SPSS 中的任何自动化解决方案都很满意。
我有大量这样的人口统计数据:
ID <- c(1, 2, 3, 4, 5)
State <- c("FL", "FL", "FL", "FL", "FL")
County <- c("Lake", "Lake", "Lake", "Orange", "Orange")
Household <- c (2, 1, 3, 2, 1)
First.Gender <- c("Male", "Female", "Male", "Female", "Male")
Second.Gender <- c("Male", "-", "Female", "Female", "-")
Third.Gender <- c("-", "-", "Male", "-", "-")
Gender_Example <- data.frame(ID, State, County, Household, First.Gender, Second.Gender, Third.Gender)
并且我想找到一种方法来根据列中的内容创建新行(而不创建空白行)。看起来像这样的东西:
ID_i <- c(1, 1, 2, 3, 3, 3, 4, 4, 5) # _i designates my ideal set
State_i <- c("FL", "FL", "FL", "FL", "FL", "FL", "FL", "FL", "FL")
County_i <- c("Lake", "Lake", "Lake", "Lake", "Lake", "Lake", "Orange", "Orange", "Orange")
Household_i <- c(2, 2, 1, 3, 3, 3, 2, 2, 1)
Gender_i <- c("Male", "Male", "Female", "Male", "Female", "Male", "Female", "Female", "Male")
Gender_ideal <- data.frame(ID_i, State_i, County_i, Household_i, Gender_i)
如果有人问过这个问题,那么我很乐意 link。谢谢!
这可能会有帮助
library(reshape2)
Gender_ideal <- melt(Gender_Example, id=c(names(Gender_Example)[1:4]))
rows.to.remove <- which(Gender_ideal$value == "-")
Gender_ideal <- Gender_ideal[-rows.to.remove,]
Gender_ideal
ID State County Household variable value
1 1 FL Lake 2 First.Gender Male
2 2 FL Lake 1 First.Gender Female
3 3 FL Lake 3 First.Gender Male
4 4 FL Orange 2 First.Gender Female
5 5 FL Orange 1 First.Gender Male
6 1 FL Lake 2 Second.Gender Male
8 3 FL Lake 3 Second.Gender Female
9 4 FL Orange 2 Second.Gender Female
13 3 FL Lake 3 Third.Gender Male
R
在 R 中,您最好的选择是 "data.table" 中的 melt
(这让您可以使用 "patterns" 来识别您的测量变量。这样,您可以:
library(data.table)
melt(setDT(Gender_Example), measure.vars = patterns("Gender$"))[value != "-"]
或者,还有 "tidyverse" 方法。
library(tidyverse)
Gender_Example %>%
gather(variable, value, ends_with("Gender")) %>%
filter(value != "-")
SPSS
在 SPSS 中,您需要查看 varstocases
。有个不错的writeup here that should help you get started.
Excel
这可能取决于您使用的 Excel 版本。如果您使用的是 2016 年,则可以对您的数据使用数据透视表 table 向导,然后双击总计以访问原本用于创建的基础 "long" table table.
过程概述at this video。
或者,您可以使用 Tableau reshaping tool as described at this video
抱歉,如果之前有人回答过这个问题,我什至不确定如何搜索它。我对 R、VBA 或 SPSS 中的任何自动化解决方案都很满意。
我有大量这样的人口统计数据:
ID <- c(1, 2, 3, 4, 5)
State <- c("FL", "FL", "FL", "FL", "FL")
County <- c("Lake", "Lake", "Lake", "Orange", "Orange")
Household <- c (2, 1, 3, 2, 1)
First.Gender <- c("Male", "Female", "Male", "Female", "Male")
Second.Gender <- c("Male", "-", "Female", "Female", "-")
Third.Gender <- c("-", "-", "Male", "-", "-")
Gender_Example <- data.frame(ID, State, County, Household, First.Gender, Second.Gender, Third.Gender)
并且我想找到一种方法来根据列中的内容创建新行(而不创建空白行)。看起来像这样的东西:
ID_i <- c(1, 1, 2, 3, 3, 3, 4, 4, 5) # _i designates my ideal set
State_i <- c("FL", "FL", "FL", "FL", "FL", "FL", "FL", "FL", "FL")
County_i <- c("Lake", "Lake", "Lake", "Lake", "Lake", "Lake", "Orange", "Orange", "Orange")
Household_i <- c(2, 2, 1, 3, 3, 3, 2, 2, 1)
Gender_i <- c("Male", "Male", "Female", "Male", "Female", "Male", "Female", "Female", "Male")
Gender_ideal <- data.frame(ID_i, State_i, County_i, Household_i, Gender_i)
如果有人问过这个问题,那么我很乐意 link。谢谢!
这可能会有帮助
library(reshape2)
Gender_ideal <- melt(Gender_Example, id=c(names(Gender_Example)[1:4]))
rows.to.remove <- which(Gender_ideal$value == "-")
Gender_ideal <- Gender_ideal[-rows.to.remove,]
Gender_ideal
ID State County Household variable value
1 1 FL Lake 2 First.Gender Male
2 2 FL Lake 1 First.Gender Female
3 3 FL Lake 3 First.Gender Male
4 4 FL Orange 2 First.Gender Female
5 5 FL Orange 1 First.Gender Male
6 1 FL Lake 2 Second.Gender Male
8 3 FL Lake 3 Second.Gender Female
9 4 FL Orange 2 Second.Gender Female
13 3 FL Lake 3 Third.Gender Male
R
在 R 中,您最好的选择是 "data.table" 中的 melt
(这让您可以使用 "patterns" 来识别您的测量变量。这样,您可以:
library(data.table)
melt(setDT(Gender_Example), measure.vars = patterns("Gender$"))[value != "-"]
或者,还有 "tidyverse" 方法。
library(tidyverse)
Gender_Example %>%
gather(variable, value, ends_with("Gender")) %>%
filter(value != "-")
SPSS
在 SPSS 中,您需要查看 varstocases
。有个不错的writeup here that should help you get started.
Excel
这可能取决于您使用的 Excel 版本。如果您使用的是 2016 年,则可以对您的数据使用数据透视表 table 向导,然后双击总计以访问原本用于创建的基础 "long" table table.
过程概述at this video。
或者,您可以使用 Tableau reshaping tool as described at this video