在大多数列具有多个值的情况下,重新变换为宽到宽(Reshape long to wide where most columns have multiple values)
我有以下数据:
IDnum zipcode City County State 10011 36006 Billingsley Autauga AL 10011 36022 Deatsville Autauga AL 10011 36051 Marbury Autauga AL 10011 36051 Prattville Autauga AL 10011 36066 Prattville Autauga AL 10011 36067 Verbena Autauga AL 10011 36091 Selma Autauga AL 10011 36703 Jones Autauga AL 10011 36749 Plantersville Autauga AL 10011 36758 Uriah Autauga AL 10011 36480 Atmore Autauga AL 10011 36502 Bon Secour Autauga AL
我有一个zipcodes列表,它们包含的城市,以及它们所在的县/州.IDnum =县和州的数值,合并。 列表是您现在看到的格式,我需要将其从长到宽/垂直到水平重新整形,其中IDnum变量成为唯一标识符,所有其他可能的值组合变为宽变量。
IDnum zip1 city1 county1 state1 zip2 city2 county2 10011 36006 Billingsley Autauga AL 36022 Deatsville Autauga
这只是数据集的示例,它包含了美国的每个zip,包含更多变量。 我已经看到了与此类似的其他问题和答案,但并不是几乎每列都有多个值的地方。
SPSS和STATA中的命令将以这种方式重塑数据,在SPSS中我可以运行Restructure / Cases to Vars命令,将我的初始数据集中的11个变量转换为大约1750,b / c一个县有超过290个拉链并且它重复大多数其他变量290+次。 这将创建许多空白,但我需要将它重新整形为一个非常长的水平文件。
我查看了reshape和reshape2,并挂断了'default to length'错误消息。 我确实得到了融合/ dcast以进行排序工作,但这会创建一个变量,它是所有值的列表,而不是为每个值创建变量。
melted_dupes <- melt(zip_code_list_dupes, id.vars= c("IDnum")) HRZ_dupes <- dcast(melted_dupes, IDnum ~ variable, fun.aggregate = list)
我尝试过tidyr和dplyr,但在语法上迷失了方向。 有点惊讶的是,没有命令数据类似于其他包中的内置命令,让我假设有,我只是没有想出来。
任何帮助表示赞赏。
I have data as below:
IDnum zipcode City County State 10011 36006 Billingsley Autauga AL 10011 36022 Deatsville Autauga AL 10011 36051 Marbury Autauga AL 10011 36051 Prattville Autauga AL 10011 36066 Prattville Autauga AL 10011 36067 Verbena Autauga AL 10011 36091 Selma Autauga AL 10011 36703 Jones Autauga AL 10011 36749 Plantersville Autauga AL 10011 36758 Uriah Autauga AL 10011 36480 Atmore Autauga AL 10011 36502 Bon Secour Autauga AL
I have a list of zipcodes, the cities they encompass, and counties/states they are located in. IDnum = numeric value for county and state, combined. List is in format you see now, I need to reshape it from long to wide / vertical to horizontal, where the IDnum variable becomes the unique identifier, and all other possible value combinations become wide variables.
IDnum zip1 city1 county1 state1 zip2 city2 county2 10011 36006 Billingsley Autauga AL 36022 Deatsville Autauga
This is just sample of the dataset, it encompasses every zip in the USA and includes more variables. I have seen other questions and answers similar to this one, but not where there are multiple values in almost every column.
There are commands in SPSS and STATA that will reshape data this way, in SPSS I can run a Restructure/Cases to Vars command that turns 11 variables in my initial dataset into about 1750, b/c one county has over 290 zips and it replicates most of the other variables 290+ times. This will create many blanks, but I need it to be reshaped into one very long horizontal file.
I have looked at reshape and reshape2, and am hung up on the 'default to length' error message. I did get melt/dcast to sorta work, but this creates one variable that is a list of all values, rather than creating variables for each value.
melted_dupes <- melt(zip_code_list_dupes, id.vars= c("IDnum")) HRZ_dupes <- dcast(melted_dupes, IDnum ~ variable, fun.aggregate = list)
I have tried tidyr and dplyr but got lost in syntax. Am a little surprised there isn't a command the data similar to built in commands in other packages, making me assume there is, and I just haven't figured it out.
Any help is appreciated.
原文:https://stackoverflow.com/questions/41982078
满意答案
方法重写必须具有与其重写的父方法相同的方法签名 ,否则它不会被重写。
Java :
public abstract class AbstractTest { public abstract void test() throws Exception; } public class ConcreteTest extends AbstractTest { @Override public void test() throws Exception { } }
正如你所看到的,
ConcreteTest
(它扩展了AbstractTest
)必须覆盖test()
。 它们具有相同的方法名称,返回类型和方法参数。 子类可以省略从基类抛出的异常并抛出它自己的异常。 该子类还可以添加额外的(未)检查异常。正如Peter Lawrey所提到的,java接口方法是隐式抽象方法(参见我在Java抽象接口上的问题 )。
这里至关重要的是,在这种情况下,方法可见性无法改变(因为它是一种层次可见性,即private-> protected-> public)。 虽然这是有效的:
public abstract class AbstractTest { protected abstract void test() throws Exception; } public class ConcreteTest extends AbstractTest { @Override public void test() throws Exception { } }
(父级拥有受保护的方法,子类可以覆盖相同的方法,并且只有2种可见性选择:受保护或公共权限)。
另外,假设你有
public class B { } public class D extends B { } public abstract class Base { public abstract B foo(); } public class Derived extends Base { @Override public D foo() { // TODO Auto-generated method stub return new D(); } }
你会看到
Derived
返回一个D
而不是B
这是为什么? 这是因为派生类遵循与父类相同的签名 ,派生类的返回类型是父类的返回类型的子类型。所以,我可以这样做:
Base pureBase = new Derived(); B b = pureBase.foo(); //which returns class D if (b instanceof D) { //sure, it is, do some other logic }
在C ++中,使用Covariant Return类型可以获得类似的效果
C ++
class AbstractTest { public: virtual void test() = 0; }; class ConcreteTest : AbstractTest { public: void test() { //Implementation here... } };
在C ++中,具有纯虚函数的类(以a
=0
结尾的虚函数)被称为Abstract类。 子类(在C ++中,类扩展由:
分隔)覆盖纯虚方法(除了不包含=0
)。 它的父类具有相同的签名。回到我们的Java示例,假设您有:
class B { }; class D : B { }; class Base { public: virtual B* foo() = 0; } class Derived : Base { public: D* foo() { return new D(); } }
在这里完成相同的推理(如java中所解释的)。 协变返回类型也适用于受保护和私有继承。 有关Covariant返回类型的更多信息。
Method overriding must have the same method signature of the parent method it's overriding, otherwise it's not called overriding.
Java:
public abstract class AbstractTest { public abstract void test() throws Exception; } public class ConcreteTest extends AbstractTest { @Override public void test() throws Exception { } }
As you can see,
ConcreteTest
(which extendsAbstractTest
) must overridetest()
. They have the same method name, return types and no method parameters. The subclass can omit the exceptions thrown from the base class and throw its own Exception. The subclass can also add additional (un)checked exception.As Peter Lawrey mentioned, a java interface methods are implicitly abstract method (See my SO question on Java Abstract Interface).
What is crucial here is that the method visibility cannot change in this case (as it's a hierarchical visibility, i.e. private->protected->public). This is valid though:
public abstract class AbstractTest { protected abstract void test() throws Exception; } public class ConcreteTest extends AbstractTest { @Override public void test() throws Exception { } }
(The parent has a protected method and the subclass can override the same method and only has 2 choice for visibility: protected or public).
Also, Suppose you have
public class B { } public class D extends B { } public abstract class Base { public abstract B foo(); } public class Derived extends Base { @Override public D foo() { // TODO Auto-generated method stub return new D(); } }
You will see that
Derived
returns aD
and not aB
. Why is that? That's because the derived class follows the same signature as the parent class and the return type of the derived class is asubtype
of the return type of the parent class.So, I can have this:
Base pureBase = new Derived(); B b = pureBase.foo(); //which returns class D if (b instanceof D) { //sure, it is, do some other logic }
In C++, you can get similar effect, using Covariant Return types
C++
class AbstractTest { public: virtual void test() = 0; }; class ConcreteTest : AbstractTest { public: void test() { //Implementation here... } };
In C++, a class with a pure virtual function (a virtual function that ends with a
=0
) is known as an Abstract class. The subclass (in C++, class extension is delimited by:
) override the pure virtual method (except it doesn't contain the=0
). It has the same signature has its parent class.Going back to our Java example, suppose you have:
class B { }; class D : B { }; class Base { public: virtual B* foo() = 0; } class Derived : Base { public: D* foo() { return new D(); } }
The same reasoning (as explained in java) is done here. Covariant return types also works with protected and private inheritance. More on Covariant return types.
相关问答
更多从C ++ / Java / C#代码中调用C方法?(Call C methods from C++/Java/C# code?)
java final方法vs c ++非虚函数(java final methods vs c++ nonvirtual functions)
在C#中使用静态类(Overiding Static Classes in C#)
如何在JNI中从C ++调用Java方法(How to call Java methods from C++ in JNI)
函数/方法vs Java的C / C ++暴露(C/C++ exposure of functions / methods vs Java)
在Objective C中模拟抽象类和抽象方法?(Simulate abstract classes and abstract methods in Objective C? [duplicate])
在超类中调用抽象方法,并在C ++中的子类中实现它?(Invoke abstract method in super class, and implement it in subclass in C++?)
为什么隐藏Java或C ++中不允许的虚函数/方法(Why isn't hiding of virtual functions/methods allowed in Java and C++?)
使用C ++中所有同步方法的类(Class with all synchronized methods in C++)
C ++和Java中的抽象方法和重写函数(abstract methods and overiding function in C++ and Java)
相关文章
更多Guava LongMath类-long基础类型的Math实用方法
java Long型的比较
Guava Longs类-long的实用工具类
Mod_python: The Long Story
grep 零宽断言
Solr4:Tomcat7与Solr之多核配置(Multiple Cores)
-bash: /bin/mv: Argument list too long的解决方案
INSERT INTO blog_appitem (user_id,appid,app_secret,is_valid) VALUES (1, 'wxf415741de036114c','48e1e345fd5f11c93af18ff1714c7f78',1)
hibernate值类型集合映射-一个顾客多个地址
Guava Booleans类-布尔型基本的实用工具类
最新问答
更多获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
如何通过引用返回对象?(How is returning an object by reference possible?)
矩阵如何存储在内存中?(How are matrices stored in memory?)
每个请求的Java新会话?(Java New Session For Each Request?)
css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
xcode语法颜色编码解释?(xcode syntax color coding explained?)
在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!