这类似于这个问题R将行数据转换为二进制列,但我想保留行数.
如何在保留行数的同时将行数据转换为二进制列?
例
输入
myData<-data.frame(gender=c("man","women","child","women","women","women","man"),
age=c(22, 22, 0.33,22,22,22,111))
myData
gender age
1 man 22.00
2 women 22.00
3 child 0.33
4 women 22.00
5 women 22.00
6 women 22.00
7 man 111.00
Run Code Online (Sandbox Code Playgroud)
如何达到这个预期的输出?
gender age man women child
1 man 22.00 1 0 0
2 women 22.00 0 1 0
3 child 0.33 0 0 1
4 women 22.00 0 1 0
5 women 22.00 0 1 0
6 women 22.00 0 1 0
7 man 111.00 1 …Run Code Online (Sandbox Code Playgroud) $ javac GetAllDirs.java
GetAllDirs.java:16: cannot find symbol
symbol : variable checkFile
location: class GetAllDirs
System.out.println(checkFile.getName());
^
1 error
$ cat GetAllDirs.java
import java.util.*;
import java.io.*;
public class GetAllDirs {
public void getAllDirs(File file) {
if(file.isDirectory()){
System.out.println(file.getName());
File checkFile = new File(file.getCanonicalPath());
}else if(file.isFile()){
System.out.println(file.getName());
File checkFile = new File(file.getParent());
}else{
// checkFile should get Initialized at least HERE!
File checkFile = file;
}
System.out.println(file.getName());
// WHY ERROR HERE: checkfile not found
System.out.println(checkFile.getName());
}
public static void main(String[] args) {
GetAllDirs …Run Code Online (Sandbox Code Playgroud) [更新,对不起有关更改,但现在对真正的问题] 我不能在那里包含try-catch-loop来获取方法getCanonicalPath()的异常.我试图先用方法解决问题,然后在那里声明值.问题是它是最终的,我无法改变它.那么如何将startingPath作为"public static final".
$ cat StartingPath.java
import java.util.*;
import java.io.*;
public class StartingPath {
public static final String startingPath = (new File(".")).getCanonicalPath();
public static void main(String[] args){
System.out.println(startingPath);
}
}
$ javac StartingPath.java
StartingPath.java:5: unreported exception java.io.IOException; must be caught or declared to be thrown
public static final String startingPath = (new File(".")).getCanonicalPath();
^
1 error
Run Code Online (Sandbox Code Playgroud) $ javac ArrayListTest.java
$ java ArrayListTest
$ cat ArrayListTest.java
import java.io.*;
import java.util.*;
public class ArrayListTest{
public static void main(String[] args) {
try {
String hello ="oeoaseu oeu hsoae sthoaust hoaeut hoasntu";
ArrayList<String> appendMe = null;
for(String s : hello.split(" "))
appendMe.add(s+" ");
for(String s : appendMe)
System.out.println(s);
//WHY DOES IT NOT PRINT?
}catch(Exception e){
}
}
}
Run Code Online (Sandbox Code Playgroud) 我知道如何通过将大小与上限进行比较来解决问题,但我想要一个查找异常的条件.如果在conditinal中发生异常,我想退出.
import java.io.*;
import java.util.*;
public class conditionalTest{
public static void main(String[] args){
Stack<Integer> numbs=new Stack<Integer>();
numbs.push(1);
numbs.push(2);
for(int count=0,j=0;try{((j=numbs.pop())<999)}catch(Exception e){break;}&&
!numbs.isEmpty(); ){
System.out.println(j);
}
// I waited for 1 to be printed, not 2.
}
}
Run Code Online (Sandbox Code Playgroud)
一些错误
javac conditionalTest.java
conditionalTest.java:10: illegal start of expression
for(int count=0,j=0;try{((j=numbs.pop())<999)}catch(Exception e){break;}&&
^
conditionalTest.java:10: illegal start of expression
for(int count=0,j=0;try{((j=numbs.pop())<999)}catch(Exception e){break;}&&
^
Run Code Online (Sandbox Code Playgroud) 我可以在命令行上尝试使用4x4绘图,但在下面的代码中不能使用for -loop和png元素.我很沮丧问题是什么,我现在已经试着理解这个时间而且无法前进.
只是$ R CMD Sweave paper.Rnw,$ pdflatex paper.tex和$ evince paper.pdf你复制后的代码下面paper.Rnw -file - ,你会得到一个1x1的情节,而不是4×4的情节.为什么?
$ mkdir Pictures
$ mkdir Data
$ cd Data
$ wget https://noppa.aalto.fi/noppa/kurssi/mat-2.3128/harjoitustyot/Mat-2_3128_data_2.xls
$ cd ..
$ cat paper.Rnw
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{graphicx}
\begin{document}
<<echo=FALSE>>=
library(gdata)
f<-read.xls('./Data/Mat-2_3128_data_2.xls')
for(i in 1:ncol(f))
{
png(paste('./Pictures/CO',i,'.png',sep =''))
par(mfrow=c(2,2))
plot(pacf(na.omit(f[,i])), main=paste('Pacf', i))
plot(acf(na.omit(f[,i])), main=paste('Acf', i))
plot(na.omit(f[,i]), main=paste('Data', i), type='l')
}
@
\subsection{Time-serie C1}
\includegraphics{./Pictures/CO1.png}
\subsection{Time-serie C2}
\includegraphics{./Pictures/CO2.png}
\subsection{Time-serie C3}
\includegraphics{./Pictures/CO3.png}
\subsection{Time-serie C4}
\includegraphics{./Pictures/CO4.png}
\subsection{Time-serie C5} …Run Code Online (Sandbox Code Playgroud) 我试图理解Matlab中的双精度数.为什么这个1 - 3*(4/3 - 1)不等于零?
floating-point matlab floating-accuracy double-precision ieee-754
假设我有以下矩阵
B =
0 1 0 0
0 1 0 1
1 0 0 0
1 1 1 0
Run Code Online (Sandbox Code Playgroud)
我想返回列的索引,其中第一个值为"0",第四个值为"1".在这种情况下,这将是(1, 3).
如何在MATLAB中获得预期的输出?
大多数答案仅针对已经回答的有关汉明重量的问题,但忽略了关于find稀疏性的观点.显然,通过夏嘉曦答案在这里解决了约找到一点-但我现在还无法验证它.我在这里的答案并没有利用其他答案的独创性,比如位移,但足够好的例子答案.
输入
>> mlf=sparse([],[],[],2^31+1,1);mlf(1)=10;mlf(10)=111;mlf(77)=1010;
>> transpose(dec2bin(find(mlf)))
ans =
001
000
000
011
001
010
101
Run Code Online (Sandbox Code Playgroud)
目标
1
0
0
2
1
1
2
Run Code Online (Sandbox Code Playgroud)
使用稀疏结构快速计算二进制数的1?
这是一个性能问题,我希望将两个独立的表中的两列组合在一起.你怎么做组合?
我理解这是or条件
SELECT a.contract1 or b.contract2 from TABLE1 a, TABLE2 b
Run Code Online (Sandbox Code Playgroud)
我的目标是获得一个列,其中每个元素都在Table1的Contract1或Table2的Contract2中.该or符号不重复值等数值进行区分.我需要不同的价值观.由于底层不同,所提出的解决方案union方法对于大量数据集而言行动缓慢.
请提出有效的方法来处理性能.
输入
表A中的列
Run Code Online (Sandbox Code Playgroud)1 2 3表B中的Golumn
Run Code Online (Sandbox Code Playgroud)1 3 5
想要输出
1
2
3
5
Run Code Online (Sandbox Code Playgroud)